Skip to content Search Apps Documentation Source Content 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.

crc32_test.gno

2.44 Kb · 89 lines
 1package crc32
 2
 3import (
 4	"strings"
 5	"testing"
 6
 7	"gno.land/p/nt/uassert/v0"
 8)
 9
10// Known-answer tests against the published IEEE vectors.
11func TestChecksumKnownVectors(t *testing.T) {
12	cases := []struct {
13		in   string
14		want string
15	}{
16		{"", "00000000"},
17		{"a", "e8b7be43"},
18		{"abc", "352441c2"},
19		{"message digest", "20159d7f"},
20		{"abcdefghijklmnopqrstuvwxyz", "4c2750bd"},
21		{"123456789", "cbf43926"}, // the standard CRC check value
22	}
23	for _, c := range cases {
24		uassert.Equal(t, c.want, ChecksumHex(c.in))
25	}
26}
27
28// The classic "check" value: CRC-32/IEEE of "123456789" is 0xCBF43926.
29func TestStandardCheckValue(t *testing.T) {
30	uassert.Equal(t, uint32(0xCBF43926), Checksum("123456789"))
31}
32
33func TestHexIsZeroPadded(t *testing.T) {
34	uassert.Equal(t, "00000000", Hex(0))
35	uassert.Equal(t, "0000000f", Hex(15))
36	uassert.Equal(t, "ffffffff", Hex(0xFFFFFFFF))
37	uassert.Equal(t, 8, len(Hex(1)))
38}
39
40// A single flipped bit must change the checksum — the entire point.
41func TestDetectsSingleBitChange(t *testing.T) {
42	base := Checksum("hello world")
43	uassert.False(t, base == Checksum("hello worle"))
44	uassert.False(t, base == Checksum("Hello world"))
45}
46
47// Order matters: a CRC is not a sum.
48func TestOrderSensitive(t *testing.T) {
49	uassert.False(t, Checksum("ab") == Checksum("ba"))
50}
51
52func TestVerify(t *testing.T) {
53	uassert.True(t, Verify("abc", 0x352441C2))
54	uassert.False(t, Verify("abd", 0x352441C2))
55}
56
57// Chaining Update over chunks must equal one shot over the whole message —
58// this is why Update takes the raw register rather than a final value.
59func TestUpdateIsChunkable(t *testing.T) {
60	whole := Checksum("hello world")
61	crc := uint32(0xFFFFFFFF)
62	crc = Update(crc, ieeeTable, "hello ")
63	crc = Update(crc, ieeeTable, "world")
64	uassert.Equal(t, whole, Finalize(crc))
65}
66
67// Different polynomials must give different answers, and each be stable.
68func TestAlternatePolynomials(t *testing.T) {
69	c := MakeTable(Castagnoli)
70	k := MakeTable(Koopman)
71	a := ChecksumWith("123456789", c)
72	b := ChecksumWith("123456789", k)
73	uassert.False(t, a == Checksum("123456789"))
74	uassert.False(t, a == b)
75	uassert.Equal(t, a, ChecksumWith("123456789", c)) // stable
76}
77
78func TestSplit(t *testing.T) {
79	got := Split("a\nabc")
80	uassert.Equal(t, 2, len(got))
81	uassert.Equal(t, "e8b7be43", got[0])
82	uassert.Equal(t, "352441c2", got[1])
83}
84
85func TestLongInput(t *testing.T) {
86	s := strings.Repeat("gno", 1000)
87	uassert.Equal(t, 8, len(ChecksumHex(s)))
88	uassert.Equal(t, Checksum(s), Checksum(s))
89}