| 
									
										
										
										
											2016-07-13 06:20:31 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2017-01-18 02:02:58 +08:00
										 |  |  |  * Minio Cloud Storage, (C) 2016, 2017 Minio, Inc. | 
					
						
							| 
									
										
										
										
											2016-07-13 06:20:31 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-07-13 06:20:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // validates functionality provided to find most common
 | 
					
						
							|  |  |  | // time occurrence from a list of time.
 | 
					
						
							|  |  |  | func TestCommonTime(t *testing.T) { | 
					
						
							|  |  |  | 	// List of test cases for common modTime.
 | 
					
						
							|  |  |  | 	testCases := []struct { | 
					
						
							|  |  |  | 		times []time.Time | 
					
						
							|  |  |  | 		time  time.Time | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// 1. Tests common times when slice has varying time elements.
 | 
					
						
							|  |  |  | 			[]time.Time{ | 
					
						
							|  |  |  | 				time.Unix(0, 1).UTC(), | 
					
						
							|  |  |  | 				time.Unix(0, 2).UTC(), | 
					
						
							|  |  |  | 				time.Unix(0, 3).UTC(), | 
					
						
							|  |  |  | 				time.Unix(0, 3).UTC(), | 
					
						
							|  |  |  | 				time.Unix(0, 2).UTC(), | 
					
						
							|  |  |  | 				time.Unix(0, 3).UTC(), | 
					
						
							|  |  |  | 				time.Unix(0, 1).UTC(), | 
					
						
							|  |  |  | 			}, time.Unix(0, 3).UTC(), | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// 2. Tests common time obtained when all elements are equal.
 | 
					
						
							|  |  |  | 			[]time.Time{ | 
					
						
							|  |  |  | 				time.Unix(0, 3).UTC(), | 
					
						
							|  |  |  | 				time.Unix(0, 3).UTC(), | 
					
						
							|  |  |  | 				time.Unix(0, 3).UTC(), | 
					
						
							|  |  |  | 				time.Unix(0, 3).UTC(), | 
					
						
							|  |  |  | 				time.Unix(0, 3).UTC(), | 
					
						
							|  |  |  | 				time.Unix(0, 3).UTC(), | 
					
						
							|  |  |  | 				time.Unix(0, 3).UTC(), | 
					
						
							|  |  |  | 			}, time.Unix(0, 3).UTC(), | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// 3. Tests common time obtained when elements have a mixture
 | 
					
						
							|  |  |  | 			// of sentinel values.
 | 
					
						
							|  |  |  | 			[]time.Time{ | 
					
						
							|  |  |  | 				time.Unix(0, 3).UTC(), | 
					
						
							|  |  |  | 				time.Unix(0, 3).UTC(), | 
					
						
							|  |  |  | 				time.Unix(0, 2).UTC(), | 
					
						
							|  |  |  | 				time.Unix(0, 1).UTC(), | 
					
						
							|  |  |  | 				time.Unix(0, 3).UTC(), | 
					
						
							|  |  |  | 				time.Unix(0, 4).UTC(), | 
					
						
							|  |  |  | 				time.Unix(0, 3).UTC(), | 
					
						
							|  |  |  | 				timeSentinel, | 
					
						
							|  |  |  | 				timeSentinel, | 
					
						
							|  |  |  | 				timeSentinel, | 
					
						
							|  |  |  | 			}, time.Unix(0, 3).UTC(), | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Tests all the testcases, and validates them against expected
 | 
					
						
							|  |  |  | 	// common modtime. Tests fail if modtime does not match.
 | 
					
						
							|  |  |  | 	for i, testCase := range testCases { | 
					
						
							|  |  |  | 		// Obtain a common mod time from modTimes slice.
 | 
					
						
							| 
									
										
										
										
											2017-01-18 02:02:58 +08:00
										 |  |  | 		ctime, _ := commonTime(testCase.times) | 
					
						
							| 
									
										
										
										
											2016-07-13 06:20:31 +08:00
										 |  |  | 		if testCase.time != ctime { | 
					
						
							|  |  |  | 			t.Fatalf("Test case %d, expect to pass but failed. Wanted modTime: %s, got modTime: %s\n", i+1, testCase.time, ctime) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |