github.com/primecitizens/pcz/std@v0.2.1/core/cmp/cmp_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright 2023 The Prime Citizens
     3  //
     4  // Copyright 2023 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  package cmp_test
     9  
    10  import (
    11  	"math"
    12  	"sort"
    13  	"testing"
    14  
    15  	"github.com/primecitizens/pcz/std/core/cmp"
    16  )
    17  
    18  var negzero = math.Copysign(0, -1)
    19  
    20  var tests = []struct {
    21  	x, y    any
    22  	compare int
    23  }{
    24  	{1, 2, -1},
    25  	{1, 1, 0},
    26  	{2, 1, +1},
    27  	{"a", "aa", -1},
    28  	{"a", "a", 0},
    29  	{"aa", "a", +1},
    30  	{1.0, 1.1, -1},
    31  	{1.1, 1.1, 0},
    32  	{1.1, 1.0, +1},
    33  	{math.Inf(1), math.Inf(1), 0},
    34  	{math.Inf(-1), math.Inf(-1), 0},
    35  	{math.Inf(-1), 1.0, -1},
    36  	{1.0, math.Inf(-1), +1},
    37  	{math.Inf(1), 1.0, +1},
    38  	{1.0, math.Inf(1), -1},
    39  	{math.NaN(), math.NaN(), 0},
    40  	{0.0, math.NaN(), +1},
    41  	{math.NaN(), 0.0, -1},
    42  	{math.NaN(), math.Inf(-1), -1},
    43  	{math.Inf(-1), math.NaN(), +1},
    44  	{0.0, 0.0, 0},
    45  	{negzero, negzero, 0},
    46  	{negzero, 0.0, 0},
    47  	{0.0, negzero, 0},
    48  	{negzero, 1.0, -1},
    49  	{negzero, -1.0, +1},
    50  }
    51  
    52  func TestLess(t *testing.T) {
    53  	for _, test := range tests {
    54  		var b bool
    55  		switch test.x.(type) {
    56  		case int:
    57  			b = cmp.Less(test.x.(int), test.y.(int))
    58  		case string:
    59  			b = cmp.Less(test.x.(string), test.y.(string))
    60  		case float64:
    61  			b = cmp.FLess(test.x.(float64), test.y.(float64))
    62  		}
    63  		if b != (test.compare < 0) {
    64  			t.Errorf("Less(%v, %v) == %t, want %t", test.x, test.y, b, test.compare < 0)
    65  		}
    66  	}
    67  }
    68  
    69  func TestCompare(t *testing.T) {
    70  	for _, test := range tests {
    71  		var c int
    72  		switch test.x.(type) {
    73  		case int:
    74  			c = cmp.Compare(test.x.(int), test.y.(int))
    75  		case string:
    76  			c = cmp.Compare(test.x.(string), test.y.(string))
    77  		case float64:
    78  			c = cmp.FCompare(test.x.(float64), test.y.(float64))
    79  		}
    80  		if c != test.compare {
    81  			t.Errorf("Compare(%v, %v) == %d, want %d", test.x, test.y, c, test.compare)
    82  		}
    83  	}
    84  }
    85  
    86  func TestSort(t *testing.T) {
    87  	// Test that our comparison function is consistent with
    88  	// sort.Float64s.
    89  	input := []float64{1.0, 0.0, negzero, math.Inf(1), math.Inf(-1), math.NaN()}
    90  	sort.Float64s(input)
    91  	for i := 0; i < len(input)-1; i++ {
    92  		if cmp.FLess(input[i+1], input[i]) {
    93  			t.Errorf("Less sort mismatch at %d in %v", i, input)
    94  		}
    95  		if cmp.FCompare(input[i], input[i+1]) > 0 {
    96  			t.Errorf("Compare sort mismatch at %d in %v", i, input)
    97  		}
    98  	}
    99  }