github.com/mymmsc/gox@v1.3.33/util/comparator_test.go (about)

     1  // Copyright (c) 2015, Emir Pasic. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package util
     6  
     7  import (
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestIntComparator(t *testing.T) {
    13  
    14  	// i1,i2,expected
    15  	tests := [][]interface{}{
    16  		{1, 1, 0},
    17  		{1, 2, -1},
    18  		{2, 1, 1},
    19  		{11, 22, -1},
    20  		{0, 0, 0},
    21  		{1, 0, 1},
    22  		{0, 1, -1},
    23  	}
    24  
    25  	for _, test := range tests {
    26  		actual := IntComparator(test[0], test[1])
    27  		expected := test[2]
    28  		if actual != expected {
    29  			t.Errorf("Got %v expected %v", actual, expected)
    30  		}
    31  	}
    32  }
    33  
    34  func TestStringComparator(t *testing.T) {
    35  
    36  	// s1,s2,expected
    37  	tests := [][]interface{}{
    38  		{"a", "a", 0},
    39  		{"a", "b", -1},
    40  		{"b", "a", 1},
    41  		{"aa", "aab", -1},
    42  		{"", "", 0},
    43  		{"a", "", 1},
    44  		{"", "a", -1},
    45  		{"", "aaaaaaa", -1},
    46  	}
    47  
    48  	for _, test := range tests {
    49  		actual := StringComparator(test[0], test[1])
    50  		expected := test[2]
    51  		if actual != expected {
    52  			t.Errorf("Got %v expected %v", actual, expected)
    53  		}
    54  	}
    55  }
    56  
    57  func TestTimeComparator(t *testing.T) {
    58  
    59  	now := time.Now()
    60  
    61  	// i1,i2,expected
    62  	tests := [][]interface{}{
    63  		{now, now, 0},
    64  		{now.Add(24 * 7 * 2 * time.Hour), now, 1},
    65  		{now, now.Add(24 * 7 * 2 * time.Hour), -1},
    66  	}
    67  
    68  	for _, test := range tests {
    69  		actual := TimeComparator(test[0], test[1])
    70  		expected := test[2]
    71  		if actual != expected {
    72  			t.Errorf("Got %v expected %v", actual, expected)
    73  		}
    74  	}
    75  }
    76  
    77  func TestCustomComparator(t *testing.T) {
    78  
    79  	type Custom struct {
    80  		id   int
    81  		name string
    82  	}
    83  
    84  	byID := func(a, b interface{}) int {
    85  		c1 := a.(Custom)
    86  		c2 := b.(Custom)
    87  		switch {
    88  		case c1.id > c2.id:
    89  			return 1
    90  		case c1.id < c2.id:
    91  			return -1
    92  		default:
    93  			return 0
    94  		}
    95  	}
    96  
    97  	// o1,o2,expected
    98  	tests := [][]interface{}{
    99  		{Custom{1, "a"}, Custom{1, "a"}, 0},
   100  		{Custom{1, "a"}, Custom{2, "b"}, -1},
   101  		{Custom{2, "b"}, Custom{1, "a"}, 1},
   102  		{Custom{1, "a"}, Custom{1, "b"}, 0},
   103  	}
   104  
   105  	for _, test := range tests {
   106  		actual := byID(test[0], test[1])
   107  		expected := test[2]
   108  		if actual != expected {
   109  			t.Errorf("Got %v expected %v", actual, expected)
   110  		}
   111  	}
   112  }