cmp_test.gno

4.72 Kb ยท 257 lines
  1package int256
  2
  3import "testing"
  4
  5func TestEq(t *testing.T) {
  6	tests := []struct {
  7		x, y string
  8		want bool
  9	}{
 10		{"0", "0", true},
 11		{"0", "1", false},
 12		{"1", "0", false},
 13		{"-1", "0", false},
 14		{"0", "-1", false},
 15		{"1", "1", true},
 16		{"-1", "-1", true},
 17		{"115792089237316195423570985008687907853269984665640564039457584007913129639935", "-115792089237316195423570985008687907853269984665640564039457584007913129639935", false},
 18		{"-115792089237316195423570985008687907853269984665640564039457584007913129639935", "-115792089237316195423570985008687907853269984665640564039457584007913129639935", true},
 19	}
 20
 21	for _, tc := range tests {
 22		x, err := FromDecimal(tc.x)
 23		if err != nil {
 24			t.Error(err)
 25			continue
 26		}
 27
 28		y, err := FromDecimal(tc.y)
 29		if err != nil {
 30			t.Error(err)
 31			continue
 32		}
 33
 34		got := x.Eq(y)
 35		if got != tc.want {
 36			t.Errorf("Eq(%s, %s) = %v, want %v", tc.x, tc.y, got, tc.want)
 37		}
 38	}
 39}
 40
 41func TestNeq(t *testing.T) {
 42	tests := []struct {
 43		x, y string
 44		want bool
 45	}{
 46		{"0", "0", false},
 47		{"0", "1", true},
 48		{"1", "0", true},
 49		{"-1", "0", true},
 50		{"0", "-1", true},
 51		{"1", "1", false},
 52		{"-1", "-1", false},
 53		{"115792089237316195423570985008687907853269984665640564039457584007913129639935", "-115792089237316195423570985008687907853269984665640564039457584007913129639935", true},
 54		{"-115792089237316195423570985008687907853269984665640564039457584007913129639935", "-115792089237316195423570985008687907853269984665640564039457584007913129639935", false},
 55	}
 56
 57	for _, tc := range tests {
 58		x, err := FromDecimal(tc.x)
 59		if err != nil {
 60			t.Error(err)
 61			continue
 62		}
 63
 64		y, err := FromDecimal(tc.y)
 65		if err != nil {
 66			t.Error(err)
 67			continue
 68		}
 69
 70		got := x.Neq(y)
 71		if got != tc.want {
 72			t.Errorf("Neq(%s, %s) = %v, want %v", tc.x, tc.y, got, tc.want)
 73		}
 74	}
 75}
 76
 77func TestCmp(t *testing.T) {
 78	tests := []struct {
 79		x, y string
 80		want int
 81	}{
 82		{"0", "0", 0},
 83		{"0", "1", -1},
 84		{"1", "0", 1},
 85		{"-1", "0", -1},
 86		{"0", "-1", 1},
 87		{"1", "1", 0},
 88		{"115792089237316195423570985008687907853269984665640564039457584007913129639935", "-115792089237316195423570985008687907853269984665640564039457584007913129639935", -1},
 89	}
 90
 91	for _, tc := range tests {
 92		x, err := FromDecimal(tc.x)
 93		if err != nil {
 94			t.Error(err)
 95			continue
 96		}
 97
 98		y, err := FromDecimal(tc.y)
 99		if err != nil {
100			t.Error(err)
101			continue
102		}
103
104		got := x.Cmp(y)
105		if got != tc.want {
106			t.Errorf("Cmp(%s, %s) = %v, want %v", tc.x, tc.y, got, tc.want)
107		}
108	}
109}
110
111func TestIsZero(t *testing.T) {
112	tests := []struct {
113		x    string
114		want bool
115	}{
116		{"0", true},
117		{"-0", true},
118		{"1", false},
119		{"-1", false},
120		{"10", false},
121	}
122
123	for _, tc := range tests {
124		x, err := FromDecimal(tc.x)
125		if err != nil {
126			t.Error(err)
127			continue
128		}
129
130		got := x.IsZero()
131		if got != tc.want {
132			t.Errorf("IsZero(%s) = %v, want %v", tc.x, got, tc.want)
133		}
134	}
135}
136
137func TestIsNeg(t *testing.T) {
138	tests := []struct {
139		x    string
140		want bool
141	}{
142		{"0", false},
143		{"-0", false},
144		{"1", false},
145		{"-1", true},
146		{"10", false},
147		{"-10", true},
148	}
149
150	for _, tc := range tests {
151		x, err := FromDecimal(tc.x)
152		if err != nil {
153			t.Error(err)
154			continue
155		}
156
157		got := x.IsNeg()
158		if got != tc.want {
159			t.Errorf("IsNeg(%s) = %v, want %v", tc.x, got, tc.want)
160		}
161	}
162}
163
164func TestLt(t *testing.T) {
165	tests := []struct {
166		x, y string
167		want bool
168	}{
169		{"0", "0", false},
170		{"0", "1", true},
171		{"1", "0", false},
172		{"-1", "0", true},
173		{"0", "-1", false},
174		{"1", "1", false},
175		{"-1", "-1", false},
176	}
177
178	for _, tc := range tests {
179		x, err := FromDecimal(tc.x)
180		if err != nil {
181			t.Error(err)
182			continue
183		}
184
185		y, err := FromDecimal(tc.y)
186		if err != nil {
187			t.Error(err)
188			continue
189		}
190
191		got := x.Lt(y)
192		if got != tc.want {
193			t.Errorf("Lt(%s, %s) = %v, want %v", tc.x, tc.y, got, tc.want)
194		}
195	}
196}
197
198func TestGt(t *testing.T) {
199	tests := []struct {
200		x, y string
201		want bool
202	}{
203		{"0", "0", false},
204		{"0", "1", false},
205		{"1", "0", true},
206		{"-1", "0", false},
207		{"0", "-1", true},
208		{"1", "1", false},
209		{"-1", "-1", false},
210	}
211
212	for _, tc := range tests {
213		x, err := FromDecimal(tc.x)
214		if err != nil {
215			t.Error(err)
216			continue
217		}
218
219		y, err := FromDecimal(tc.y)
220		if err != nil {
221			t.Error(err)
222			continue
223		}
224
225		got := x.Gt(y)
226		if got != tc.want {
227			t.Errorf("Gt(%s, %s) = %v, want %v", tc.x, tc.y, got, tc.want)
228		}
229	}
230}
231
232func TestClone(t *testing.T) {
233	tests := []string{
234		"0",
235		"-0",
236		"1",
237		"-1",
238		"10",
239		"-10",
240		"115792089237316195423570985008687907853269984665640564039457584007913129639935",
241		"-115792089237316195423570985008687907853269984665640564039457584007913129639935",
242	}
243
244	for _, xStr := range tests {
245		x, err := FromDecimal(xStr)
246		if err != nil {
247			t.Error(err)
248			continue
249		}
250
251		y := x.Clone()
252
253		if x.Neq(y) {
254			t.Errorf("cloned value is not equal to original value")
255		}
256	}
257}