github.com/thiagoyeds/go-cloud@v0.26.0/docstore/driver/compare_test.go (about)

     1  // Copyright 2019 The Go Cloud Development Kit Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package driver
    16  
    17  import (
    18  	"math"
    19  	"testing"
    20  	"time"
    21  )
    22  
    23  func TestCompareTimes(t *testing.T) {
    24  	t1 := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
    25  	t2 := t1.Add(1)
    26  	for _, test := range []struct {
    27  		in1, in2 time.Time
    28  		want     int
    29  	}{
    30  		{t1, t2, -1},
    31  		{t2, t1, 1},
    32  		{t1, t1, 0},
    33  	} {
    34  		got := CompareTimes(test.in1, test.in2)
    35  		if got != test.want {
    36  			t.Errorf("CompareTimes(%v, %v) == %d, want %d", test.in1, test.in2, got, test.want)
    37  		}
    38  	}
    39  }
    40  
    41  func TestCompareNumbers(t *testing.T) {
    42  	check := func(n1, n2 interface{}, want int) {
    43  		t.Helper()
    44  		got, err := CompareNumbers(n1, n2)
    45  		if err != nil {
    46  			t.Fatalf("CompareNumbers(%T(%[1]v), %T(%[2]v)): %v", n1, n2, err)
    47  		}
    48  		if got != want {
    49  			t.Errorf("CompareNumbers(%T(%[1]v), %T(%[2]v)) = %d, want %d", n1, n2, got, want)
    50  		}
    51  	}
    52  
    53  	for _, test := range []struct {
    54  		in1, in2 interface{}
    55  		want     int
    56  	}{
    57  		// simple cases
    58  		{1, 1, 0},
    59  		{1, 2, -1},
    60  		{1.0, 1.0, 0},
    61  		{1.0, 2.0, -1},
    62  
    63  		// mixed int types
    64  		{int8(1), int64(1), 0},
    65  		{int8(2), int64(1), 1},
    66  		{uint(1), int(1), 0},
    67  		{uint(2), int(1), 1},
    68  
    69  		// mixed int and float
    70  		{1, 1.0, 0},
    71  		{1, 1.1, -1},
    72  		{2, 1.1, 1},
    73  
    74  		// large numbers
    75  		{int64(math.MaxInt64), int64(math.MaxInt64), 0},
    76  		{uint64(math.MaxUint64), uint64(math.MaxUint64), 0},
    77  		{float64(math.MaxFloat64), float64(math.MaxFloat64), 0},
    78  		{int64(math.MaxInt64), int64(math.MaxInt64 - 1), 1},
    79  		{int64(math.MaxInt64), float64(math.MaxInt64 - 1), -1}, // float is bigger because it gets rounded up
    80  		{int64(math.MaxInt64), uint64(math.MaxUint64), -1},
    81  
    82  		// special floats
    83  		{int64(math.MaxInt64), math.Inf(1), -1},
    84  		{int64(math.MinInt64), math.Inf(-1), 1},
    85  	} {
    86  		check(test.in1, test.in2, test.want)
    87  		if test.want != 0 {
    88  			check(test.in2, test.in1, -test.want)
    89  		}
    90  	}
    91  }