Luigit
repositories / bugabinga.net

bugabinga.net

personal infrastructure for bugabinga!

owned by admin

services/luci/internal/proquint/proquint_test.go

Raw
package proquint

import "testing"

func TestRoundTrip(t *testing.T) {
	for _, id := range []int64{0, 1, 1790457879211659274, -1, 9223372036854775807} {
		encoded := Encode(id)
		decoded, ok := Decode(encoded)
		if !ok || decoded != id {
			t.Fatalf("round trip %d: %q -> %d, %v", id, encoded, decoded, ok)
		}
	}
}

func TestKnownVectorFromPaper(t *testing.T) {
	// 127.0.0.1 encodes as lusab-babad per the specification.
	bytes, ok := PrefixBytes("lusab-babad")
	if !ok {
		t.Fatal("paper vector rejected")
	}
	if len(bytes) != 4 || bytes[0] != 127 || bytes[1] != 0 || bytes[2] != 0 || bytes[3] != 1 {
		t.Fatalf("bytes = %v", bytes)
	}
}

func TestPrefixBytes(t *testing.T) {
	full := Encode(42)
	bytes, ok := PrefixBytes(full[:5])
	if !ok || len(bytes) != 2 {
		t.Fatalf("single block prefix: %v, %v", bytes, ok)
	}
	if _, ok := PrefixBytes("kud"); ok {
		t.Fatal("short block accepted")
	}
	if _, ok := PrefixBytes("kudab-lusab-babad-fugab-kabab"); ok {
		t.Fatal("five blocks accepted")
	}
	if _, ok := PrefixBytes("xudab"); ok {
		t.Fatal("non-consonant accepted")
	}
}

func TestDecodeRejectsPrefixes(t *testing.T) {
	if _, ok := Decode(Encode(7)[:11]); ok {
		t.Fatal("partial decode accepted")
	}
}