github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/libnetwork/ipbits/ipbits_test.go (about)

     1  package ipbits
     2  
     3  import (
     4  	"net/netip"
     5  	"testing"
     6  )
     7  
     8  func TestAdd(t *testing.T) {
     9  	tests := []struct {
    10  		in    netip.Addr
    11  		x     uint64
    12  		shift uint
    13  		want  netip.Addr
    14  	}{
    15  		{netip.MustParseAddr("10.0.0.1"), 0, 0, netip.MustParseAddr("10.0.0.1")},
    16  		{netip.MustParseAddr("10.0.0.1"), 41, 0, netip.MustParseAddr("10.0.0.42")},
    17  		{netip.MustParseAddr("10.0.0.1"), 42, 16, netip.MustParseAddr("10.42.0.1")},
    18  		{netip.MustParseAddr("10.0.0.1"), 1, 7, netip.MustParseAddr("10.0.0.129")},
    19  		{netip.MustParseAddr("10.0.0.1"), 1, 24, netip.MustParseAddr("11.0.0.1")},
    20  		{netip.MustParseAddr("2001::1"), 0, 0, netip.MustParseAddr("2001::1")},
    21  		{netip.MustParseAddr("2001::1"), 0x41, 0, netip.MustParseAddr("2001::42")},
    22  		{netip.MustParseAddr("2001::1"), 1, 7, netip.MustParseAddr("2001::81")},
    23  		{netip.MustParseAddr("2001::1"), 0xcafe, 96, netip.MustParseAddr("2001:cafe::1")},
    24  		{netip.MustParseAddr("2001::1"), 1, 112, netip.MustParseAddr("2002::1")},
    25  	}
    26  
    27  	for _, tt := range tests {
    28  		if got := Add(tt.in, tt.x, tt.shift); tt.want != got {
    29  			t.Errorf("%v + (%v << %v) = %v; want %v", tt.in, tt.x, tt.shift, got, tt.want)
    30  		}
    31  	}
    32  }
    33  
    34  func BenchmarkAdd(b *testing.B) {
    35  	do := func(b *testing.B, addr netip.Addr) {
    36  		b.ReportAllocs()
    37  		for i := 0; i < b.N; i++ {
    38  			_ = Add(addr, uint64(i), 0)
    39  		}
    40  	}
    41  
    42  	b.Run("IPv4", func(b *testing.B) { do(b, netip.IPv4Unspecified()) })
    43  	b.Run("IPv6", func(b *testing.B) { do(b, netip.IPv6Unspecified()) })
    44  }
    45  
    46  func TestField(t *testing.T) {
    47  	tests := []struct {
    48  		in   netip.Addr
    49  		u, v uint
    50  		want uint64
    51  	}{
    52  		{netip.MustParseAddr("1.2.3.4"), 0, 8, 1},
    53  		{netip.MustParseAddr("1.2.3.4"), 8, 16, 2},
    54  		{netip.MustParseAddr("1.2.3.4"), 16, 24, 3},
    55  		{netip.MustParseAddr("1.2.3.4"), 24, 32, 4},
    56  		{netip.MustParseAddr("1.2.3.4"), 0, 32, 0x01020304},
    57  		{netip.MustParseAddr("1.2.3.4"), 0, 28, 0x102030},
    58  		{netip.MustParseAddr("1234:5678:9abc:def0::7654:3210"), 0, 8, 0x12},
    59  		{netip.MustParseAddr("1234:5678:9abc:def0::7654:3210"), 8, 16, 0x34},
    60  		{netip.MustParseAddr("1234:5678:9abc:def0::7654:3210"), 16, 24, 0x56},
    61  		{netip.MustParseAddr("1234:5678:9abc:def0::7654:3210"), 64, 128, 0x76543210},
    62  		{netip.MustParseAddr("1234:5678:9abc:def0:beef::7654:3210"), 48, 80, 0xdef0beef},
    63  	}
    64  
    65  	for _, tt := range tests {
    66  		if got := Field(tt.in, tt.u, tt.v); got != tt.want {
    67  			t.Errorf("Field(%v, %v, %v) = %v (0x%[4]x); want %v (0x%[5]x)", tt.in, tt.u, tt.v, got, tt.want)
    68  		}
    69  	}
    70  }