equal_test.go 4.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
package ipld_test

import (
	"testing"

	"github.com/ipfs/go-cid"
	"github.com/ipld/go-ipld-prime"
	"github.com/ipld/go-ipld-prime/fluent/qp"
	cidlink "github.com/ipld/go-ipld-prime/linking/cid"
	basic "github.com/ipld/go-ipld-prime/node/basic" // shorter name for the tests
)

var (
	globalNode = basic.NewString("global")
	globalLink = func() ipld.Link {
		someCid, _ := cid.Cast([]byte{1, 85, 0, 5, 0, 1, 2, 3, 4})
		return cidlink.Link{Cid: someCid}
	}()
	globalLink2 = func() ipld.Link {
		someCid, _ := cid.Cast([]byte{1, 85, 0, 5, 0, 5, 6, 7, 8})
		return cidlink.Link{Cid: someCid}
	}()
)

func qpMust(node ipld.Node, err error) ipld.Node {
	if err != nil {
		panic(err)
	}
	return node
}

var deepEqualTests = []struct {
	name        string
	left, right ipld.Node
	want        bool
}{
	{"MismatchingKinds", basic.NewBool(true), basic.NewInt(3), false},

	{"SameNodeSamePointer", globalNode, globalNode, true},
	// Repeated basicnode.New invocations might return different pointers.
	{"SameNodeDiffPointer", basic.NewString("same"), basic.NewString("same"), true},

	{"SameKindNull", ipld.Null, ipld.Null, true},
	{"DiffKindNull", ipld.Null, ipld.Absent, false},
	{"SameKindBool", basic.NewBool(true), basic.NewBool(true), true},
	{"DiffKindBool", basic.NewBool(true), basic.NewBool(false), false},
	{"SameKindInt", basic.NewInt(12), basic.NewInt(12), true},
	{"DiffKindInt", basic.NewInt(12), basic.NewInt(15), false},
	{"SameKindFloat", basic.NewFloat(1.25), basic.NewFloat(1.25), true},
	{"DiffKindFloat", basic.NewFloat(1.25), basic.NewFloat(1.75), false},
	{"SameKindString", basic.NewString("foobar"), basic.NewString("foobar"), true},
	{"DiffKindString", basic.NewString("foobar"), basic.NewString("baz"), false},
	{"SameKindBytes", basic.NewBytes([]byte{5, 2, 3}), basic.NewBytes([]byte{5, 2, 3}), true},
	{"DiffKindBytes", basic.NewBytes([]byte{5, 2, 3}), basic.NewBytes([]byte{5, 8, 3}), false},
	{"SameKindLink", basic.NewLink(globalLink), basic.NewLink(globalLink), true},
	{"DiffKindLink", basic.NewLink(globalLink), basic.NewLink(globalLink2), false},

	{
		"SameKindList",
		qpMust(qp.BuildList(basic.Prototype.Any, -1, func(am ipld.ListAssembler) {
			qp.ListEntry(am, qp.Int(7))
			qp.ListEntry(am, qp.Int(8))
		})),
		qpMust(qp.BuildList(basic.Prototype.Any, -1, func(am ipld.ListAssembler) {
			qp.ListEntry(am, qp.Int(7))
			qp.ListEntry(am, qp.Int(8))
		})),
		true,
	},
	{
		"DiffKindList_length",
		qpMust(qp.BuildList(basic.Prototype.Any, -1, func(am ipld.ListAssembler) {
			qp.ListEntry(am, qp.Int(7))
			qp.ListEntry(am, qp.Int(8))
		})),
		qpMust(qp.BuildList(basic.Prototype.Any, -1, func(am ipld.ListAssembler) {
			qp.ListEntry(am, qp.Int(7))
		})),
		false,
	},
	{
		"DiffKindList_elems",
		qpMust(qp.BuildList(basic.Prototype.Any, -1, func(am ipld.ListAssembler) {
			qp.ListEntry(am, qp.Int(7))
			qp.ListEntry(am, qp.Int(8))
		})),
		qpMust(qp.BuildList(basic.Prototype.Any, -1, func(am ipld.ListAssembler) {
			qp.ListEntry(am, qp.Int(3))
			qp.ListEntry(am, qp.Int(2))
		})),
		false,
	},

	{
		"SameKindMap",
		qpMust(qp.BuildMap(basic.Prototype.Any, -1, func(am ipld.MapAssembler) {
			qp.MapEntry(am, "foo", qp.Int(7))
			qp.MapEntry(am, "bar", qp.Int(8))
		})),
		qpMust(qp.BuildMap(basic.Prototype.Any, -1, func(am ipld.MapAssembler) {
			qp.MapEntry(am, "foo", qp.Int(7))
			qp.MapEntry(am, "bar", qp.Int(8))
		})),
		true,
	},
	{
		"DiffKindMap_length",
		qpMust(qp.BuildMap(basic.Prototype.Any, -1, func(am ipld.MapAssembler) {
			qp.MapEntry(am, "foo", qp.Int(7))
			qp.MapEntry(am, "bar", qp.Int(8))
		})),
		qpMust(qp.BuildMap(basic.Prototype.Any, -1, func(am ipld.MapAssembler) {
			qp.MapEntry(am, "foo", qp.Int(7))
		})),
		false,
	},
	{
		"DiffKindMap_elems",
		qpMust(qp.BuildMap(basic.Prototype.Any, -1, func(am ipld.MapAssembler) {
			qp.MapEntry(am, "foo", qp.Int(7))
			qp.MapEntry(am, "bar", qp.Int(8))
		})),
		qpMust(qp.BuildMap(basic.Prototype.Any, -1, func(am ipld.MapAssembler) {
			qp.MapEntry(am, "foo", qp.Int(3))
			qp.MapEntry(am, "baz", qp.Int(8))
		})),
		false,
	},

	// TODO: tests involving different implementations, once bindnode is ready

}

func TestDeepEqual(t *testing.T) {
	t.Parallel()
	for _, tc := range deepEqualTests {
		tc := tc // capture range variable
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()

			got := ipld.DeepEqual(tc.left, tc.right)
			if got != tc.want {
				t.Fatalf("DeepEqual got %v, want %v", got, tc.want)
			}
		})
	}
}