Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

uint256_test.gno

2.89 Kb · 127 lines
  1package uint256
  2
  3import (
  4	"testing"
  5)
  6
  7func TestSetAllOne(t *testing.T) {
  8	z := Zero()
  9	z.SetAllOne()
 10	if z.String() != twoPow256Sub1 {
 11		t.Errorf("Expected all ones, got %s", z.String())
 12	}
 13}
 14
 15func TestByte(t *testing.T) {
 16	tests := []struct {
 17		input    string
 18		position uint64
 19		expected byte
 20	}{
 21		{"0x1000000000000000000000000000000000000000000000000000000000000000", 0, 16},
 22		{"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0, 255},
 23		{"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 31, 255},
 24	}
 25
 26	for i, tt := range tests {
 27		z, _ := FromHex(tt.input)
 28		n := NewUint(tt.position)
 29		result := z.Byte(n)
 30
 31		if result.arr[0] != uint64(tt.expected) {
 32			t.Errorf("Test case %d failed. Input: %s, Position: %d, Expected: %d, Got: %d",
 33				i, tt.input, tt.position, tt.expected, result.arr[0])
 34		}
 35
 36		// check other array elements are 0
 37		if result.arr[1] != 0 || result.arr[2] != 0 || result.arr[3] != 0 {
 38			t.Errorf("Test case %d failed. Non-zero values in upper bytes", i)
 39		}
 40	}
 41
 42	// overflow
 43	z, _ := FromHex("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
 44	n := NewUint(32)
 45	result := z.Byte(n)
 46
 47	if !result.IsZero() {
 48		t.Errorf("Expected zero for position >= 32, got %v", result)
 49	}
 50}
 51
 52func TestBitLen(t *testing.T) {
 53	tests := []struct {
 54		input    string
 55		expected int
 56	}{
 57		{"0x0", 0},
 58		{"0x1", 1},
 59		{"0xff", 8},
 60		{"0x100", 9},
 61		{"0xffff", 16},
 62		{"0x10000", 17},
 63		{"0xffffffffffffffff", 64},
 64		{"0x10000000000000000", 65},
 65		{"0xffffffffffffffffffffffffffffffff", 128},
 66		{"0x100000000000000000000000000000000", 129},
 67		{"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 256},
 68	}
 69
 70	for i, tt := range tests {
 71		z, _ := FromHex(tt.input)
 72		result := z.BitLen()
 73
 74		if result != tt.expected {
 75			t.Errorf("Test case %d failed. Input: %s, Expected: %d, Got: %d",
 76				i, tt.input, tt.expected, result)
 77		}
 78	}
 79}
 80
 81func TestByteLen(t *testing.T) {
 82	tests := []struct {
 83		input    string
 84		expected int
 85	}{
 86		{"0x0", 0},
 87		{"0x1", 1},
 88		{"0xff", 1},
 89		{"0x100", 2},
 90		{"0xffff", 2},
 91		{"0x10000", 3},
 92		{"0xffffffffffffffff", 8},
 93		{"0x10000000000000000", 9},
 94		{"0xffffffffffffffffffffffffffffffff", 16},
 95		{"0x100000000000000000000000000000000", 17},
 96		{"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32},
 97	}
 98
 99	for i, tt := range tests {
100		z, _ := FromHex(tt.input)
101		result := z.ByteLen()
102
103		if result != tt.expected {
104			t.Errorf("Test case %d failed. Input: %s, Expected: %d, Got: %d",
105				i, tt.input, tt.expected, result)
106		}
107	}
108}
109
110func TestClone(t *testing.T) {
111	tests := []struct {
112		input    string
113		expected string
114	}{
115		{"0x1", "1"},
116		{"0x100", "256"},
117		{"0x10000000000000000", "18446744073709551616"},
118	}
119
120	for _, tt := range tests {
121		z, _ := FromHex(tt.input)
122		result := z.Clone()
123		if result.String() != tt.expected {
124			t.Errorf("Test %s failed. Expected %s, got %s", tt.input, tt.expected, result.String())
125		}
126	}
127}