github.com/primecitizens/pcz/std@v0.2.1/builtin/int/int128_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 // 4 // Copyright 2020 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 stdint 9 10 import ( 11 "testing" 12 ) 13 14 func TestUint128AddSub(t *testing.T) { 15 const add1 = 1 16 const sub1 = -1 17 tests := []struct { 18 in Uint128 19 op int // +1 or -1 to add vs subtract 20 want Uint128 21 }{ 22 {Uint128{hi: 0, lo: 0}, add1, Uint128{hi: 0, lo: 1}}, 23 {Uint128{hi: 0, lo: 1}, add1, Uint128{hi: 0, lo: 2}}, 24 {Uint128{hi: 1, lo: 0}, add1, Uint128{hi: 1, lo: 1}}, 25 {Uint128{hi: 0, lo: ^uint64(0)}, add1, Uint128{hi: 1, lo: 0}}, 26 {Uint128{hi: ^uint64(0), lo: ^uint64(0)}, add1, Uint128{hi: 0, lo: 0}}, 27 28 {Uint128{hi: 0, lo: 0}, sub1, Uint128{hi: ^uint64(0), lo: ^uint64(0)}}, 29 {Uint128{hi: 0, lo: 1}, sub1, Uint128{hi: 0, lo: 0}}, 30 {Uint128{hi: 0, lo: 2}, sub1, Uint128{hi: 0, lo: 1}}, 31 {Uint128{hi: 1, lo: 0}, sub1, Uint128{hi: 0, lo: ^uint64(0)}}, 32 {Uint128{hi: 1, lo: 1}, sub1, Uint128{hi: 1, lo: 0}}, 33 } 34 for _, tt := range tests { 35 var got Uint128 36 switch tt.op { 37 case add1: 38 got = tt.in.AddOne() 39 case sub1: 40 got = tt.in.SubOne() 41 default: 42 panic("bogus op") 43 } 44 if got != tt.want { 45 t.Errorf("%v add %d = %v; want %v", tt.in, tt.op, got, tt.want) 46 } 47 } 48 } 49 50 func TestBitsSetFrom(t *testing.T) { 51 tests := []struct { 52 bit uint8 53 want Uint128 54 }{ 55 {0, Uint128{hi: ^uint64(0), lo: ^uint64(0)}}, 56 {1, Uint128{hi: ^uint64(0) >> 1, lo: ^uint64(0)}}, 57 {63, Uint128{hi: 1, lo: ^uint64(0)}}, 58 {64, Uint128{hi: 0, lo: ^uint64(0)}}, 59 {65, Uint128{hi: 0, lo: ^uint64(0) >> 1}}, 60 {127, Uint128{hi: 0, lo: 1}}, 61 {128, Uint128{hi: 0, lo: 0}}, 62 } 63 for _, tt := range tests { 64 var zero Uint128 65 got := zero.bitsSetFrom(tt.bit) 66 if got != tt.want { 67 t.Errorf("0.bitsSetFrom(%d) = %064b want %064b", tt.bit, got, tt.want) 68 } 69 } 70 } 71 72 func TestBitsClearedFrom(t *testing.T) { 73 tests := []struct { 74 bit uint8 75 want Uint128 76 }{ 77 {0, Uint128{hi: 0, lo: 0}}, 78 {1, Uint128{hi: 1 << 63, lo: 0}}, 79 {63, Uint128{hi: ^uint64(0) &^ 1, lo: 0}}, 80 {64, Uint128{hi: ^uint64(0), lo: 0}}, 81 {65, Uint128{hi: ^uint64(0), lo: 1 << 63}}, 82 {127, Uint128{hi: ^uint64(0), lo: ^uint64(0) &^ 1}}, 83 {128, Uint128{hi: ^uint64(0), lo: ^uint64(0)}}, 84 } 85 for _, tt := range tests { 86 ones := Uint128{hi: ^uint64(0), lo: ^uint64(0)} 87 got := ones.bitsClearedFrom(tt.bit) 88 if got != tt.want { 89 t.Errorf("ones.bitsClearedFrom(%d) = %064b want %064b", tt.bit, got, tt.want) 90 } 91 } 92 }