Example #1
0
func TestNewCursor(t *testing.T) {
	for i, tt := range newCursorTests {
		in, orig := toPrefixes(tt.in), toPrefixes(tt.in)
		ipaddr.NewCursor(in)
		if !reflect.DeepEqual(in, orig) {
			t.Errorf("#%d: %v is corrupted; want %v", i, in, orig)
		}
	}

	ipaddr.NewCursor(nil)
}
Example #2
0
func TestCursorFirstLastIP(t *testing.T) {
	for i, tt := range cursorFirstLastIPTests {
		c := ipaddr.NewCursor(tt.in)
		fpos, lpos := c.First(), c.Last()
		if !tt.first.Equal(fpos.IP) || !tt.last.Equal(lpos.IP) {
			t.Errorf("#%d: got %v, %v; want %v, %v", i, fpos.IP, lpos.IP, tt.first, tt.last)
		}
	}
}
Example #3
0
func TestCursorFirstLastPrefix(t *testing.T) {
	for i, tt := range cursorFirstLastPrefixTests {
		c := ipaddr.NewCursor(tt.in)
		fpos, lpos := c.First(), c.Last()
		if !tt.first.Equal(&fpos.Prefix) || !tt.last.Equal(&lpos.Prefix) {
			t.Errorf("#%d: got %v, %v; want %v, %v", i, fpos.Prefix, lpos.Prefix, tt.first, tt.last)
		}
	}
}
Example #4
0
func BenchmarkCursorPrevIPv4(b *testing.B) {
	ps := toPrefixes([]string{"192.168.0.255/24"})
	c := ipaddr.NewCursor(ps)

	for i := 0; i < b.N; i++ {
		for c.Prev() != nil {
		}
	}
}
Example #5
0
func BenchmarkCursorNextIPv6(b *testing.B) {
	ps := toPrefixes([]string{"2001:db8::/120"})
	c := ipaddr.NewCursor(ps)

	for i := 0; i < b.N; i++ {
		for c.Next() != nil {
		}
	}
}
Example #6
0
func BenchmarkCursorNextIPv4(b *testing.B) {
	ps := toPrefixes([]string{"192.0.2.0/24"})
	c := ipaddr.NewCursor(ps)

	for i := 0; i < b.N; i++ {
		for c.Next() != nil {
		}
	}
}
Example #7
0
func TestCursorReset(t *testing.T) {
	for i, tt := range cursorResetTests {
		in := toPrefixes([]string{"0.0.0.0/0"})
		c := ipaddr.NewCursor(in)
		c.Reset(tt.in)
		if !reflect.DeepEqual(c.Pos(), tt.out) {
			t.Errorf("#%d: got %v; want %v", i, c.Pos(), tt.out)
		}
	}
}
Example #8
0
func TestCursorPos(t *testing.T) {
	for i, tt := range cursorPosTests {
		c := ipaddr.NewCursor(tt.ps)
		err := c.Set(tt.in)
		if err != nil && tt.error == nil {
			t.Errorf("#%d: got %v; want %v", i, err, tt.error)
		}
		if err != nil {
			continue
		}
		if !reflect.DeepEqual(c.Pos(), tt.in) {
			t.Errorf("#%d: got %v; want %v", i, c.Pos(), tt.in)
		}
	}
}
Example #9
0
func TestCursorPrevNext(t *testing.T) {
	for i, tt := range cursorPrevNextTests {
		c := ipaddr.NewCursor(tt.ps)
		if err := c.Set(tt.in); err != nil {
			t.Fatal(err)
		}
		out := c.Prev()
		if !reflect.DeepEqual(out, tt.pwant) {
			t.Errorf("#%d: got %v; want %v", i, out, tt.pwant)
		}
		if err := c.Set(tt.in); err != nil {
			t.Fatal(err)
		}
		out = c.Next()
		if !reflect.DeepEqual(out, tt.nwant) {
			t.Errorf("#%d: got %v; want %v", i, out, tt.nwant)
		}
	}
}