1
0

range_test.go 712 B

12345678910111213141516171819202122232425262728293031
  1. package addrgen
  2. import (
  3. "math/big"
  4. "net/netip"
  5. "testing"
  6. )
  7. func must[T any](x T, err error) T {
  8. if err != nil {
  9. panic(err)
  10. }
  11. return x
  12. }
  13. func TestAddrRangeSingle(t *testing.T) {
  14. for _, sample := range []string{"127.0.0.1", "0.0.0.0", "::1", "255.255.255.255", "::"} {
  15. a := netip.MustParseAddr(sample)
  16. r := must(NewAddrRange(a, a))
  17. if res := r.Addr(); res != sample {
  18. t.Errorf("expected: %q; got: %q", sample, res)
  19. }
  20. }
  21. }
  22. func TestAddrRangePower(t *testing.T) {
  23. r := must(NewAddrRange(netip.MustParseAddr("127.0.0.1"), netip.MustParseAddr("127.0.0.10")))
  24. if res := r.Power(); big.NewInt(10).Cmp(res) != 0 {
  25. t.Errorf("expected: %s, got: %s", big.NewInt(10).String(), res.String())
  26. }
  27. }