map_test.go 4.46 KB
Newer Older
Eric Myhre's avatar
Eric Myhre committed
1 2 3
package impls

import (
4
	"strconv"
Eric Myhre's avatar
Eric Myhre committed
5 6 7
	"testing"

	wish "github.com/warpfork/go-wish"
8 9 10

	ipld "github.com/ipld/go-ipld-prime/_rsrch/nodesolution"
	"github.com/ipld/go-ipld-prime/must"
Eric Myhre's avatar
Eric Myhre committed
11 12
)

13 14 15 16 17 18 19 20 21 22 23 24 25 26
var tableStrInt = [25]struct {
	s string
	i int
}{}

func init() {
	for i := 1; i <= 25; i++ {
		tableStrInt[i-1] = struct {
			s string
			i int
		}{"k" + strconv.Itoa(i), i}
	}
}

Eric Myhre's avatar
Eric Myhre committed
27 28 29 30 31 32 33
func TestGennedMapIntValues(t *testing.T) {
	CheckMaps(t, Type__Map_K_T{})
}
func TestGenericMapIntValues(t *testing.T) {
	CheckMaps(t, Style__Map{})
}

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
func buildMapStrIntN3(ns ipld.NodeStyle) ipld.Node {
	nb := ns.NewBuilder()
	ma, err := nb.BeginMap(3)
	must.NotError(err)
	must.NotError(ma.AssembleKey().AssignString("whee"))
	must.NotError(ma.AssembleValue().AssignInt(1))
	must.NotError(ma.AssembleKey().AssignString("woot"))
	must.NotError(ma.AssembleValue().AssignInt(2))
	must.NotError(ma.AssembleKey().AssignString("waga"))
	must.NotError(ma.AssembleValue().AssignInt(3))
	must.NotError(ma.Done())
	n, err := nb.Build()
	must.NotError(err)
	return n
}

50 51 52 53 54 55 56 57 58 59 60 61 62 63
func buildMapStrIntN25(ns ipld.NodeStyle) ipld.Node {
	nb := ns.NewBuilder()
	ma, err := nb.BeginMap(25)
	must.NotError(err)
	for i := 1; i <= 25; i++ {
		must.NotError(ma.AssembleKey().AssignString(tableStrInt[i-1].s))
		must.NotError(ma.AssembleValue().AssignInt(tableStrInt[i-1].i))
	}
	must.NotError(ma.Done())
	n, err := nb.Build()
	must.NotError(err)
	return n
}

Eric Myhre's avatar
Eric Myhre committed
64 65
func CheckMaps(t *testing.T, ns ipld.NodeStyle) {
	t.Run("map node, str:int, 3 entries", func(t *testing.T) {
66
		n := buildMapStrIntN3(ns)
Eric Myhre's avatar
Eric Myhre committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
		t.Run("reads back out", func(t *testing.T) {
			wish.Wish(t, n.Length(), wish.ShouldEqual, 3)

			v, err := n.LookupString("whee")
			wish.Wish(t, err, wish.ShouldEqual, nil)
			v2, err := v.AsInt()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			wish.Wish(t, v2, wish.ShouldEqual, 1)

			v, err = n.LookupString("waga")
			wish.Wish(t, err, wish.ShouldEqual, nil)
			v2, err = v.AsInt()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			wish.Wish(t, v2, wish.ShouldEqual, 3)

			v, err = n.LookupString("woot")
			wish.Wish(t, err, wish.ShouldEqual, nil)
			v2, err = v.AsInt()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			wish.Wish(t, v2, wish.ShouldEqual, 2)
		})
		t.Run("reads via iteration", func(t *testing.T) {
			itr := n.MapIterator()

			wish.Wish(t, itr.Done(), wish.ShouldEqual, false)
			k, v, err := itr.Next()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			k2, err := k.AsString()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			wish.Wish(t, k2, wish.ShouldEqual, "whee")
			v2, err := v.AsInt()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			wish.Wish(t, v2, wish.ShouldEqual, 1)

			wish.Wish(t, itr.Done(), wish.ShouldEqual, false)
			k, v, err = itr.Next()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			k2, err = k.AsString()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			wish.Wish(t, k2, wish.ShouldEqual, "woot")
			v2, err = v.AsInt()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			wish.Wish(t, v2, wish.ShouldEqual, 2)

			wish.Wish(t, itr.Done(), wish.ShouldEqual, false)
			k, v, err = itr.Next()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			k2, err = k.AsString()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			wish.Wish(t, k2, wish.ShouldEqual, "waga")
			v2, err = v.AsInt()
			wish.Wish(t, err, wish.ShouldEqual, nil)
			wish.Wish(t, v2, wish.ShouldEqual, 3)

			wish.Wish(t, itr.Done(), wish.ShouldEqual, true)
			k, v, err = itr.Next()
			wish.Wish(t, err, wish.ShouldEqual, ipld.ErrIteratorOverread{})
			wish.Wish(t, k, wish.ShouldEqual, nil)
			wish.Wish(t, v, wish.ShouldEqual, nil)
		})
		t.Run("reads for absent keys error sensibly", func(t *testing.T) {
			v, err := n.LookupString("nope")
129
			wish.Wish(t, err, wish.ShouldBeSameTypeAs, ipld.ErrNotExists{})
Eric Myhre's avatar
Eric Myhre committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
			wish.Wish(t, err.Error(), wish.ShouldEqual, `key not found: "nope"`)
			wish.Wish(t, v, wish.ShouldEqual, nil)
		})
	})
	t.Run("repeated key should error", func(t *testing.T) {
		nb := ns.NewBuilder()
		ma, err := nb.BeginMap(3)
		if err != nil {
			panic(err)
		}
		if err := ma.AssembleKey().AssignString("whee"); err != nil {
			panic(err)
		}
		if err := ma.AssembleValue().AssignInt(1); err != nil {
			panic(err)
		}
		if err := ma.AssembleKey().AssignString("whee"); err != nil {
147
			wish.Wish(t, err, wish.ShouldBeSameTypeAs, ipld.ErrRepeatedMapKey{})
Eric Myhre's avatar
Eric Myhre committed
148 149 150 151 152 153 154 155
			// No string assertion at present -- how that should be presented for typed stuff is unsettled
			//  (and if it's clever, it'll differ from untyped, which will mean no assertion possible!).
		}
	})
	t.Run("builder reset works", func(t *testing.T) {
		// TODO
	})
}