netaddr_setoid_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  5. */
  6. package netaddr
  7. import (
  8. "testing"
  9. )
  10. func TestNetAddrSetoidReturnsRelativeComplement(t *testing.T) {
  11. na0, _ := IP("192.0.2.0/25")
  12. na1, _ := IP("192.0.2.128/25")
  13. na2, _ := IP("192.0.2.0/24")
  14. na3, _ := IP("192.0.2.126/31")
  15. s0 := NewNetAddrSetoid([]*NetAddr{})
  16. s1 := s0.Insert(na0).Insert(na1)
  17. if !s1.Blocks()[0].IsEqual(na2) {
  18. t.Errorf("Insert: expected %s; got %s", na2, s1.Blocks()[0])
  19. return
  20. }
  21. s2 := NewNetAddrSetoid([]*NetAddr{na3})
  22. s3 := RelComp(s1, s2)
  23. s3b := NewNetAddrSetoid([]*NetAddr{
  24. &NetAddr{[]byte{192, 0, 2, 0}, uint8(26)},
  25. &NetAddr{[]byte{192, 0, 2, 64}, uint8(27)},
  26. &NetAddr{[]byte{192, 0, 2, 96}, uint8(28)},
  27. &NetAddr{[]byte{192, 0, 2, 112}, uint8(29)},
  28. &NetAddr{[]byte{192, 0, 2, 120}, uint8(30)},
  29. &NetAddr{[]byte{192, 0, 2, 124}, uint8(31)},
  30. &NetAddr{[]byte{192, 0, 2, 128}, uint8(25)},
  31. })
  32. if !s3.IsEqual(s3b) {
  33. t.Errorf("RelComp:\nexpected\n%s;\ngot\n%s", s3b, s3)
  34. }
  35. s4 := Union(s3, s2)
  36. if !s1.IsEqual(s4) {
  37. t.Errorf("Union: expected %+v; got %+v", s1, s4)
  38. }
  39. }