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

int256_test.gno

4.33 Kb · 213 lines
  1package int256
  2
  3import (
  4	"testing"
  5
  6	"gno.land/p/onbloc/uint256/v0"
  7)
  8
  9func TestInitializers(t *testing.T) {
 10	tests := []struct {
 11		name     string
 12		fn       func() *Int
 13		wantSign int
 14		wantStr  string
 15	}{
 16		{"Zero", Zero, 0, "0"},
 17		{"New", New, 0, "0"},
 18		{"One", One, 1, "1"},
 19	}
 20
 21	for _, tt := range tests {
 22		t.Run(tt.name, func(t *testing.T) {
 23			z := tt.fn()
 24			if z.Sign() != tt.wantSign {
 25				t.Errorf("%s() = %d, want %d", tt.name, z.Sign(), tt.wantSign)
 26			}
 27			if z.String() != tt.wantStr {
 28				t.Errorf("%s() = %s, want %s", tt.name, z.String(), tt.wantStr)
 29			}
 30		})
 31	}
 32}
 33
 34func TestNewInt(t *testing.T) {
 35	tests := []struct {
 36		input    int64
 37		expected int
 38	}{
 39		{0, 0},
 40		{1, 1},
 41		{-1, -1},
 42		{9223372036854775807, 1},   // max int64
 43		{-9223372036854775808, -1}, // min int64
 44	}
 45
 46	for _, tt := range tests {
 47		z := NewInt(tt.input)
 48		if z.Sign() != tt.expected {
 49			t.Errorf("NewInt(%d) = %d, want %d", tt.input, z.Sign(), tt.expected)
 50		}
 51	}
 52}
 53
 54func TestFromDecimal(t *testing.T) {
 55	tests := []struct {
 56		input    string
 57		expected int
 58		isError  bool
 59	}{
 60		{"0", 0, false},
 61		{"1", 1, false},
 62		{"-1", -1, false},
 63		{"123456789", 1, false},
 64		{"-123456789", -1, false},
 65		{"invalid", 0, true},
 66	}
 67
 68	for _, tt := range tests {
 69		z, err := FromDecimal(tt.input)
 70		if tt.isError {
 71			if err == nil {
 72				t.Errorf("FromDecimal(%s) expected error, but got nil", tt.input)
 73			}
 74		} else {
 75			if err != nil {
 76				t.Errorf("FromDecimal(%s) unexpected error: %v", tt.input, err)
 77			} else if z.Sign() != tt.expected {
 78				t.Errorf("FromDecimal(%s) sign is incorrect. Expected: %d, Actual: %d", tt.input, tt.expected, z.Sign())
 79			}
 80		}
 81	}
 82}
 83
 84func TestMustFromDecimal(t *testing.T) {
 85	tests := []struct {
 86		input       string
 87		expected    int
 88		shouldPanic bool
 89	}{
 90		{"0", 0, false},
 91		{"1", 1, false},
 92		{"-1", -1, false},
 93		{"123", 1, false},
 94		{"invalid", 0, true},
 95	}
 96
 97	for _, tt := range tests {
 98		if tt.shouldPanic {
 99			defer func() {
100				if r := recover(); r == nil {
101					t.Errorf("MustFromDecimal(%q) expected panic, but got nil", tt.input)
102				}
103			}()
104		}
105
106		z := MustFromDecimal(tt.input)
107		if !tt.shouldPanic && z.Sign() != tt.expected {
108			t.Errorf("MustFromDecimal(%q) sign is incorrect. Expected: %d, Actual: %d", tt.input, tt.expected, z.Sign())
109		}
110	}
111}
112
113func TestSetUint64(t *testing.T) {
114	tests := []uint64{
115		0,
116		1,
117		18446744073709551615, // max uint64
118	}
119
120	for _, tt := range tests {
121		z := New().SetUint64(tt)
122		if z.Sign() < 0 {
123			t.Errorf("SetUint64(%d) result is negative", tt)
124		}
125		if tt == 0 && z.Sign() != 0 {
126			t.Errorf("SetUint64(0) result is not zero")
127		}
128		if tt > 0 && z.Sign() != 1 {
129			t.Errorf("SetUint64(%d) result is not positive", tt)
130		}
131	}
132}
133
134func TestFromUint256(t *testing.T) {
135	tests := []struct {
136		input    *uint256.Uint
137		expected int
138	}{
139		{uint256.NewUint(0), 0},
140		{uint256.NewUint(1), 1},
141		{uint256.NewUint(18446744073709551615), 1},
142	}
143
144	for _, tt := range tests {
145		z := New().FromUint256(tt.input)
146		if z.Sign() != tt.expected {
147			t.Errorf("FromUint256(%v) = %d, want %d", tt.input, z.Sign(), tt.expected)
148		}
149	}
150}
151
152func TestSign(t *testing.T) {
153	tests := []struct {
154		x    string
155		want int
156	}{
157		{"0", 0},
158		{"-0", 0},
159		{"+0", 0},
160		{"1", 1},
161		{"-1", -1},
162		{"9223372036854775807", 1},
163		{"-9223372036854775808", -1},
164	}
165
166	for _, tt := range tests {
167		z := MustFromDecimal(tt.x)
168		got := z.Sign()
169		if got != tt.want {
170			t.Errorf("Sign(%s) = %d, want %d", tt.x, got, tt.want)
171		}
172	}
173}
174
175func BenchmarkSign(b *testing.B) {
176	z := New()
177	for i := 0; i < b.N; i++ {
178		z.SetUint64(uint64(i))
179		z.Sign()
180	}
181}
182
183func TestSetAndToString(t *testing.T) {
184	tests := []struct {
185		input    string
186		expected int
187		isError  bool
188	}{
189		{"0", 0, false},
190		{"1", 1, false},
191		{"-1", -1, false},
192		{"123456789", 1, false},
193		{"-123456789", -1, false},
194		{"invalid", 0, true},
195	}
196
197	for _, tt := range tests {
198		z, err := New().SetString(tt.input)
199		if tt.isError {
200			if err == nil {
201				t.Errorf("SetString(%s) expected error, but got nil", tt.input)
202			}
203		} else {
204			if err != nil {
205				t.Errorf("SetString(%s) unexpected error: %v", tt.input, err)
206			} else if z.Sign() != tt.expected {
207				t.Errorf("SetString(%s) sign is incorrect. Expected: %d, Actual: %d", tt.input, tt.expected, z.Sign())
208			} else if z.String() != tt.input {
209				t.Errorf("SetString(%s) string representation is incorrect. Expected: %s, Actual: %s", tt.input, tt.input, z.String())
210			}
211		}
212	}
213}