github.com/Ptt-official-app/go-bbs@v0.12.0/pttbbs/internal/fnv/fnv_test.go (about)

     1  package fnv
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestFnv32a(t *testing.T) {
     8  	type Case struct {
     9  		input    string
    10  		offset   uint32
    11  		expected uint32
    12  	}
    13  
    14  	testcases := []Case{
    15  		{
    16  			input:    "",
    17  			offset:   0x12345678,
    18  			expected: 0x12345678,
    19  		},
    20  		{
    21  			input:    "",
    22  			offset:   0x811c9dc5,
    23  			expected: 0x811c9dc5,
    24  		},
    25  		{
    26  			// from golang golden32
    27  			input:    "abc",
    28  			offset:   0x811c9dc5,
    29  			expected: 0x1a47e90b,
    30  		},
    31  		{
    32  			input:    "12312",
    33  			offset:   PttFnv32Init,
    34  			expected: 0x7AEB94B2,
    35  		},
    36  		{
    37  			input:    "PICHU",
    38  			offset:   PttFnv32Init,
    39  			expected: 0xA3389082,
    40  		},
    41  		{
    42  			input:    "12312",
    43  			offset:   12345,
    44  			expected: 0x2d7500e0,
    45  		},
    46  	}
    47  
    48  	for _, c := range testcases {
    49  		h := New32aWith(c.offset)
    50  		h.Write([]byte(c.input))
    51  		b := []byte{}
    52  		b = h.Sum(b)
    53  		var actual uint32
    54  		t.Log("b:", b)
    55  		actual = uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3])
    56  		if actual != c.expected {
    57  			t.Errorf("fav32 hash failed, expected: 0x%X, got 0x%X", c.expected, actual)
    58  		}
    59  
    60  	}
    61  
    62  }