github.com/enetx/g@v1.0.80/tests/cmp_test.go (about)

     1  package g_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/enetx/g/cmp"
     7  )
     8  
     9  func TestMinBy(t *testing.T) {
    10  	// Test case 1: Minimum integer
    11  	minInt := cmp.MinBy(cmp.Cmp, 3, 1, 4, 2, 5)
    12  	expectedMinInt := 1
    13  	if minInt != expectedMinInt {
    14  		t.Errorf("MinBy(IntCompare, 3, 1, 4, 2, 5) = %d; want %d", minInt, expectedMinInt)
    15  	}
    16  
    17  	// Test case 2: Minimum string
    18  	minString := cmp.MinBy(cmp.Cmp, "banana", "apple", "orange")
    19  	expectedMinString := "apple"
    20  	if minString != expectedMinString {
    21  		t.Errorf("MinBy(StringCompare, \"banana\", \"apple\", \"orange\") = %s; want %s", minString, expectedMinString)
    22  	}
    23  }
    24  
    25  func TestMaxBy(t *testing.T) {
    26  	// Test case 1: Maximum integer
    27  	maxInt := cmp.MaxBy(cmp.Cmp, 3, 1, 4, 2, 5)
    28  	expectedMaxInt := 5
    29  	if maxInt != expectedMaxInt {
    30  		t.Errorf("MaxBy(IntCompare, 3, 1, 4, 2, 5) = %d; want %d", maxInt, expectedMaxInt)
    31  	}
    32  
    33  	// Test case 2: Maximum string
    34  	maxString := cmp.MaxBy(cmp.Cmp, "banana", "apple", "orange")
    35  	expectedMaxString := "orange"
    36  	if maxString != expectedMaxString {
    37  		t.Errorf("MaxBy(StringCompare, \"banana\", \"apple\", \"orange\") = %s; want %s", maxString, expectedMaxString)
    38  	}
    39  }
    40  
    41  func TestOrderingIsLt(t *testing.T) {
    42  	if !cmp.Less.IsLt() {
    43  		t.Errorf("Expected Less to be less than other values")
    44  	}
    45  	if cmp.Equal.IsLt() {
    46  		t.Errorf("Expected Equal not to be less than other values")
    47  	}
    48  	if cmp.Greater.IsLt() {
    49  		t.Errorf("Expected Greater not to be less than other values")
    50  	}
    51  }
    52  
    53  func TestOrderingIsEq(t *testing.T) {
    54  	if !cmp.Equal.IsEq() {
    55  		t.Errorf("Expected Equal to be equal to itself")
    56  	}
    57  	if cmp.Less.IsEq() {
    58  		t.Errorf("Expected Less not to be equal to other values")
    59  	}
    60  	if cmp.Greater.IsEq() {
    61  		t.Errorf("Expected Greater not to be equal to other values")
    62  	}
    63  }
    64  
    65  func TestOrderingIsGt(t *testing.T) {
    66  	if !cmp.Greater.IsGt() {
    67  		t.Errorf("Expected Greater to be greater than other values")
    68  	}
    69  	if cmp.Equal.IsGt() {
    70  		t.Errorf("Expected Equal not to be greater than other values")
    71  	}
    72  	if cmp.Less.IsGt() {
    73  		t.Errorf("Expected Less not to be greater than other values")
    74  	}
    75  }
    76  
    77  func TestMaxAndMin(t *testing.T) {
    78  	// Test cases for Max function
    79  	maxInt := cmp.Max(1, 2, 3, 4, 5)
    80  	if maxInt != 5 {
    81  		t.Errorf("cmp.Max(1, 2, 3, 4, 5) = %d; want 5", maxInt)
    82  	}
    83  
    84  	maxFloat := cmp.Max(1.5, 2.5, 3.5, 4.5, 5.5)
    85  	if maxFloat != 5.5 {
    86  		t.Errorf("cmp.Max(1.5, 2.5, 3.5, 4.5, 5.5) = %f; want 5.5", maxFloat)
    87  	}
    88  
    89  	maxempt := cmp.Max[int]()
    90  	if maxempt != 0 {
    91  		t.Errorf("cmp.Max() = %d; want 0", maxempt)
    92  	}
    93  
    94  	// Test cases for Min function
    95  	minInt := cmp.Min(5, 4, 3, 2, 1)
    96  	if minInt != 1 {
    97  		t.Errorf("cmp.Min(5, 4, 3, 2, 1) = %d; want 1", minInt)
    98  	}
    99  
   100  	minFloat := cmp.Min(5.5, 4.5, 3.5, 2.5, 1.5)
   101  	if minFloat != 1.5 {
   102  		t.Errorf("cmp.Min(5.5, 4.5, 3.5, 2.5, 1.5) = %f; want 1.5", minFloat)
   103  	}
   104  
   105  	minempt := cmp.Min[int]()
   106  	if minempt != 0 {
   107  		t.Errorf("cmp.Min() = %d; want 0", minempt)
   108  	}
   109  }
   110  
   111  func TestOrderingThen(t *testing.T) {
   112  	tests := []struct {
   113  		name     string
   114  		o        cmp.Ordering
   115  		other    cmp.Ordering
   116  		expected cmp.Ordering
   117  	}{
   118  		{"Non-zero receiver", cmp.Ordering(2), cmp.Ordering(3), cmp.Ordering(2)},
   119  		{"Zero receiver", cmp.Ordering(0), cmp.Ordering(3), cmp.Ordering(3)},
   120  	}
   121  
   122  	for _, tt := range tests {
   123  		t.Run(tt.name, func(t *testing.T) {
   124  			result := tt.o.Then(tt.other)
   125  			if result != tt.expected {
   126  				t.Errorf("Expected %v, but got %v", tt.expected, result)
   127  			}
   128  		})
   129  	}
   130  }
   131  
   132  func TestOrderingCmp(t *testing.T) {
   133  	tests := []struct {
   134  		name     string
   135  		x        cmp.Ordering
   136  		y        cmp.Ordering
   137  		expected cmp.Ordering
   138  	}{
   139  		{"x < y", cmp.Ordering(2), cmp.Ordering(3), cmp.Ordering(-1)},
   140  		{"x = y", cmp.Ordering(2), cmp.Ordering(2), cmp.Ordering(0)},
   141  		{"x > y", cmp.Ordering(3), cmp.Ordering(2), cmp.Ordering(1)},
   142  	}
   143  
   144  	for _, tt := range tests {
   145  		t.Run(tt.name, func(t *testing.T) {
   146  			result := cmp.Cmp(tt.x, tt.y)
   147  			if result != tt.expected {
   148  				t.Errorf("Expected %v, but got %v", tt.expected, result)
   149  			}
   150  		})
   151  	}
   152  }
   153  
   154  func TestOrderingReverse(t *testing.T) {
   155  	tests := []struct {
   156  		name     string
   157  		input    cmp.Ordering
   158  		expected cmp.Ordering
   159  	}{
   160  		{"Reverse_Less", cmp.Less, cmp.Greater},
   161  		{"Reverse_Equal", cmp.Equal, cmp.Equal},
   162  		{"Reverse_Greater", cmp.Greater, cmp.Less},
   163  	}
   164  
   165  	for _, tt := range tests {
   166  		t.Run(tt.name, func(t *testing.T) {
   167  			result := tt.input.Reverse()
   168  			if result != tt.expected {
   169  				t.Errorf("Expected reverse of %v to be %v, but got %v", tt.input, tt.expected, result)
   170  			}
   171  		})
   172  	}
   173  }
   174  
   175  func TestOrderingString(t *testing.T) {
   176  	tests := []struct {
   177  		name     string
   178  		input    cmp.Ordering
   179  		expected string
   180  	}{
   181  		{"Less", cmp.Less, "Less"},
   182  		{"Equal", cmp.Equal, "Equal"},
   183  		{"Greater", cmp.Greater, "Greater"},
   184  		{"Unknown", 10, "Unknown Ordering value: 10"},
   185  	}
   186  
   187  	for _, tt := range tests {
   188  		t.Run(tt.name, func(t *testing.T) {
   189  			result := tt.input.String()
   190  			if result != tt.expected {
   191  				t.Errorf("Expected %s for Ordering value %d, but got %s", tt.expected, tt.input, result)
   192  			}
   193  		})
   194  	}
   195  }