github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/uint128/uint128_test.go (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package uint128
    12  
    13  import (
    14  	"bytes"
    15  	"strings"
    16  	"testing"
    17  )
    18  
    19  func TestBytes(t *testing.T) {
    20  	b := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
    21  
    22  	i := FromBytes(b)
    23  
    24  	if !bytes.Equal(i.GetBytes(), b) {
    25  		t.Errorf("incorrect bytes representation for num: %v", i)
    26  	}
    27  }
    28  
    29  func TestString(t *testing.T) {
    30  	s := "a95e31998f38490651c02b97c7f2acca"
    31  
    32  	i, _ := FromString(s)
    33  
    34  	if s != i.String() {
    35  		t.Errorf("incorrect string representation for num: %v", i)
    36  	}
    37  }
    38  
    39  func TestStringTooLong(t *testing.T) {
    40  	s := "ba95e31998f38490651c02b97c7f2acca"
    41  
    42  	_, err := FromString(s)
    43  
    44  	if err == nil || !strings.Contains(err.Error(), "too large") {
    45  		t.Error("did not get error for encoding invalid uint128 string")
    46  	}
    47  }
    48  
    49  func TestStringInvalidHex(t *testing.T) {
    50  	s := "bazz95e31998849051c02b97c7f2acca"
    51  
    52  	_, err := FromString(s)
    53  
    54  	if err == nil || !strings.Contains(err.Error(), "could not decode") {
    55  		t.Error("did not get error for encoding invalid uint128 string")
    56  	}
    57  }
    58  
    59  func TestSub(t *testing.T) {
    60  	testData := []struct {
    61  		num      Uint128
    62  		expected Uint128
    63  		sub      uint64
    64  	}{
    65  		{Uint128{0, 1}, Uint128{0, 0}, 1},
    66  		{Uint128{18446744073709551615, 18446744073709551615}, Uint128{18446744073709551615, 18446744073709551614}, 1},
    67  		{Uint128{0, 18446744073709551615}, Uint128{0, 18446744073709551614}, 1},
    68  		{Uint128{18446744073709551615, 0}, Uint128{18446744073709551614, 18446744073709551615}, 1},
    69  		{Uint128{18446744073709551615, 0}, Uint128{18446744073709551614, 18446744073709551591}, 25},
    70  	}
    71  
    72  	for _, test := range testData {
    73  		res := test.num.Sub(test.sub)
    74  		if res != test.expected {
    75  			t.Errorf("expected: %v - %d = %v but got %v", test.num, test.sub, test.expected, res)
    76  		}
    77  	}
    78  }
    79  
    80  func TestAdd(t *testing.T) {
    81  	testData := []struct {
    82  		num      Uint128
    83  		expected Uint128
    84  		add      uint64
    85  	}{
    86  		{Uint128{0, 0}, Uint128{0, 1}, 1},
    87  		{Uint128{18446744073709551615, 18446744073709551614}, Uint128{18446744073709551615, 18446744073709551615}, 1},
    88  		{Uint128{0, 18446744073709551615}, Uint128{1, 0}, 1},
    89  		{Uint128{18446744073709551615, 0}, Uint128{18446744073709551615, 1}, 1},
    90  		{Uint128{0, 18446744073709551615}, Uint128{1, 24}, 25},
    91  	}
    92  
    93  	for _, test := range testData {
    94  		res := test.num.Add(test.add)
    95  		if res != test.expected {
    96  			t.Errorf("expected: %v + %d = %v but got %v", test.num, test.add, test.expected, res)
    97  		}
    98  	}
    99  }
   100  
   101  func TestEqual(t *testing.T) {
   102  	testData := []struct {
   103  		u1       Uint128
   104  		u2       Uint128
   105  		expected bool
   106  	}{
   107  		{Uint128{0, 0}, Uint128{0, 1}, false},
   108  		{Uint128{1, 0}, Uint128{0, 1}, false},
   109  		{Uint128{18446744073709551615, 18446744073709551614}, Uint128{18446744073709551615, 18446744073709551615}, false},
   110  		{Uint128{0, 1}, Uint128{0, 1}, true},
   111  		{Uint128{0, 0}, Uint128{0, 0}, true},
   112  		{Uint128{314, 0}, Uint128{314, 0}, true},
   113  		{Uint128{18446744073709551615, 18446744073709551615}, Uint128{18446744073709551615, 18446744073709551615}, true},
   114  	}
   115  
   116  	for _, test := range testData {
   117  
   118  		if actual := test.u1.Equal(test.u2); actual != test.expected {
   119  			t.Errorf("expected: %v.Equal(%v) expected %v but got %v", test.u1, test.u2, test.expected, actual)
   120  		}
   121  	}
   122  }
   123  
   124  func TestAnd(t *testing.T) {
   125  	u1 := Uint128{14799720563850130797, 11152134164166830811}
   126  	u2 := Uint128{10868624793753271583, 6542293553298186666}
   127  
   128  	expected := Uint128{9529907221165552909, 1927615693132931210}
   129  	if !(u1.And(u2)).Equal(expected) {
   130  		t.Errorf("incorrect AND computation: %v & %v != %v", u1, u2, expected)
   131  	}
   132  }
   133  
   134  func TestOr(t *testing.T) {
   135  	u1 := Uint128{14799720563850130797, 11152134164166830811}
   136  	u2 := Uint128{10868624793753271583, 6542293553298186666}
   137  
   138  	expected := Uint128{16138438136437849471, 15766812024332086267}
   139  	if !(u1.Or(u2)).Equal(expected) {
   140  		t.Errorf("incorrect OR computation: %v | %v != %v", u1, u2, expected)
   141  	}
   142  }
   143  
   144  func TestXor(t *testing.T) {
   145  	u1 := Uint128{14799720563850130797, 11152134164166830811}
   146  	u2 := Uint128{10868624793753271583, 6542293553298186666}
   147  
   148  	expected := Uint128{6608530915272296562, 13839196331199155057}
   149  	if !(u1.Xor(u2)).Equal(expected) {
   150  		t.Errorf("incorrect XOR computation: %v ^ %v != %v", u1, u2, expected)
   151  	}
   152  }