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

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

solrt_test.gno

2.54 Kb · 91 lines
 1package solrt
 2
 3import "testing"
 4
 5func TestU256Arithmetic(t *testing.T) {
 6	a := U256FromString("1000000000000000000000000") // 1e24
 7	b := U256FromString("250000000000000000000000")  // 2.5e23
 8
 9	sum := a.Add(b)
10	if sum.String() != "1250000000000000000000000" {
11		t.Fatalf("Add mismatch: got %s", sum.String())
12	}
13
14	diff := a.Sub(b)
15	if diff.String() != "750000000000000000000000" {
16		t.Fatalf("Sub mismatch: got %s", diff.String())
17	}
18
19	prod := U256FromU64(1000000000000).Mul(U256FromU64(1000000000000))
20	if prod.String() != "1000000000000000000000000" {
21		t.Fatalf("Mul mismatch: got %s", prod.String())
22	}
23
24	// MulDiv sin overflow intermedio (x*y > 2^256)
25	big := U256FromU64(10).Pow(40)
26	md := MulDiv(big, big, big)
27	if !md.Eq(big) {
28		t.Fatalf("MulDiv mismatch: got %s want %s", md.String(), big.String())
29	}
30}
31
32func TestOrderedMap(t *testing.T) {
33	m := NewOrderedMap()
34	if !m.GetU256("missing").IsZero() {
35		t.Fatalf("expected zero value for missing key")
36	}
37
38	m.SetU256("alice", U256FromU64(500))
39	m.SetU256("bob", U256FromU64(250))
40	m.SetU256("charlie", U256FromU64(100))
41	m.SetU256("alice", U256FromU64(750))
42
43	if m.Size() != 3 {
44		t.Fatalf("expected size 3, got %d", m.Size())
45	}
46	if m.GetU256("alice").Uint64() != 750 {
47		t.Fatalf("expected alice=750, got %s", m.GetU256("alice").String())
48	}
49}
50
51func expectSolrtPanic(t *testing.T, label string, fn func()) {
52	t.Helper()
53	panicked := false
54	func() {
55		defer func() {
56			if recover() != nil {
57				panicked = true
58			}
59		}()
60		fn()
61	}()
62	if !panicked {
63		t.Fatalf("%s: expected panic", label)
64	}
65}
66
67func TestExactWidthOverflowChecks(t *testing.T) {
68	if SafeAddU8(200, 55) != 255 {
69		t.Fatalf("SafeAddU8(200, 55) != 255")
70	}
71	expectSolrtPanic(t, "uint8 overflow", func() { SafeAddU8(255, 1) })
72	expectSolrtPanic(t, "uint16 overflow", func() { SafeAddU16(65535, 1) })
73	expectSolrtPanic(t, "uint32 overflow", func() { SafeAddU32(4294967295, 1) })
74
75	// uint128 límite exacto: 2^128 - 1 OK, +1 panic
76	maxU128 := U256FromU64(1).Pow(0)
77	_ = maxU128
78	two128 := U256FromU64(2).Pow(128)
79	two128Minus1 := two128.Sub(U256FromU64(1))
80	two128Minus1.CheckBits(128) // OK
81	expectSolrtPanic(t, "uint128 overflow", func() { two128.CheckBits(128) })
82
83	// Signed checked arithmetic
84	if SafeSubI64(10, 25) != -15 {
85		t.Fatalf("SafeSubI64(10, 25) != -15")
86	}
87	expectSolrtPanic(t, "int8 overflow", func() { SafeAddI8(127, 1) })
88	expectSolrtPanic(t, "int8 underflow", func() { SafeSubI8(-128, 1) })
89	expectSolrtPanic(t, "int64 min/-1 div overflow", func() { SafeDivI64(minInt64, -1) })
90	expectSolrtPanic(t, "int64 min neg overflow", func() { SafeNegI64(minInt64) })
91}