PathrockNetwork Gno Explorer
HomeBlocksTransactionsTokensRealmsPackagesValidatorsAnalytics

PathrockNetwork Gno Explorer — an independent explorer for Gno.land Mainnet (gnoland-1), operated by PathrockNetwork. Not an official Gno.land service.

gnowebarchive RPC

gno.land/p/onbloc/int256/v0

Package
Open in gnoweb ↗

Overview

Kind
Pure package
Name
v0
Namespace
onbloc / int256
Files
12 (gnomod.toml)
Exported functions
n/a — not supported for pure packages by the node (vm/qfuncs)
Module
gno.land/p/onbloc/int256/v0
gno
0.9

Files (12)

  • gnomod.tomltoml
  • arithmetic.gnogno
  • bitwise.gnogno
  • cmp.gnogno
  • conversion.gnogno
  • doc.gnogno
  • int256.gnogno
  • arithmetic_test.gnogno
  • bitwise_test.gnogno
  • cmp_test.gnogno
  • conversion_test.gnogno
  • int256_test.gnogno
  • int256_test.gnogno
    1package int25623import (4	"testing"56	"gno.land/p/onbloc/uint256/v0"7)8

    Functions

    not supported for pure packages by the node (vm/qfuncs)

    Signatures reconstructed verbatim from vm/qfuncs — interface params keep their inline definitions.

    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}
    214