package rings import ( "fmt" "net/netip" "testing" ) func TestDecodeRingZeroAddress(t *testing.T) { RZNDecodeTest(t, "DecodeRingZeroAddress", DecodeRingZeroAddress, []RZNDecodeTestCase{ {1, 1, 50, MustParseAddr("10.0.17.50"), true}, {1, 2, 50, MustParseAddr("10.0.18.50"), true}, {2, 3, 1, MustParseAddr("10.0.35.1"), true}, }) } func TesDecodetRingOneAddress(t *testing.T) { RZNDecodeTest(t, "DecodeRingOneAddress", DecodeRingOneAddress, []RZNDecodeTestCase{ {1, 1, 50, MustParseAddr("10.1.16.50"), true}, {1, 2, 50, MustParseAddr("10.1.32.50"), true}, {2, 3, 300, MustParseAddr("10.2.49.44"), true}, }) } type RZNDecodeTestCase struct { region RegionID zone ZoneID node NodeID addr netip.Addr ok bool } func RZNDecodeTest(t *testing.T, fnName string, fn func(netip.Addr) (RegionID, ZoneID, NodeID, bool), cases []RZNDecodeTestCase) { // for i, tc := range cases { s := fmt.Sprintf("%s(%q)", fnName, tc.addr) r, z, n, ok := fn(tc.addr) switch { case ok != tc.ok, r != tc.region, z != tc.zone, n != tc.node: t.Errorf("ERROR: [%v/%v]: %s → %v %v %v %v (expected %v %v %v %v)", i, len(cases), s, r, z, n, ok, tc.region, tc.zone, tc.node, tc.ok) default: t.Logf("[%v/%v]: %s → %v %v %v %v", i, len(cases), s, r, z, n, ok) } } }