func TestAppendMarshalUnmarshalGet(x *testing.T) {
	t := (*T)(x)
	SIZE := 100
	list := New(10)
	items := make([]types.Int, 0, SIZE)
	for i := 0; i < SIZE; i++ {
		item := types.Int(rand.Intn(10) + 1)
		items = append(items, item)
		t.assert_nil(list.Append(item))
	}
	for i, item := range items {
		lg, err := list.Get(i)
		t.assert_nil(err)
		t.assert(fmt.Sprintf("i %v, items[i] == list.Get(i)", i), lg.Equals(item))
	}
	marshal, unmarshal := types.IntMarshals()
	mlist1 := NewMList(list, marshal, unmarshal)
	bytes, err := mlist1.MarshalBinary()
	t.assert_nil(err)
	mlist2 := &MList{MarshalItem: marshal, UnmarshalItem: unmarshal}
	t.assert_nil(mlist2.UnmarshalBinary(bytes))
	for i, item := range items {
		lg, err := mlist2.Get(i)
		t.assert_nil(err)
		t.assert(fmt.Sprintf("i %v, items[i] == list.Get(i)", i), lg.Equals(item))
	}
}
示例#2
0
func TestSortedAddMarshalUnmarshalGet(x *testing.T) {
	t := (*T)(x)
	SIZE := 100
	list := NewSorted(10, false)
	items := make([]types.Int, 0, SIZE)
	for i := 0; i < SIZE; i++ {
		item := types.Int(rand.Intn(10) + 1)
		items = append(items, item)
		t.assert_nil(list.Add(item))
	}
	for _, item := range items {
		i, has, err := list.Find(item)
		t.assert("has", has)
		t.assert_nil(err)
		lg, err := list.Get(i)
		t.assert_nil(err)
		t.assert(fmt.Sprintf("i %v, items[i] == list.Get(i), %v, %v", i, item, lg), lg.Equals(item))
	}
	marshal, unmarshal := types.IntMarshals()
	mlist1 := NewMSorted(list, marshal, unmarshal)
	bytes, err := mlist1.MarshalBinary()
	t.assert_nil(err)
	mlist2 := &MSorted{MList: MList{MarshalItem: marshal, UnmarshalItem: unmarshal}, AllowDups: false}
	t.assert_nil(mlist2.UnmarshalBinary(bytes))
	list2 := mlist2.Sorted()
	for _, item := range items {
		i, has, err := list2.Find(item)
		t.assert("has", has)
		t.assert_nil(err)
		lg, err := list2.Get(i)
		t.assert_nil(err)
		t.assert(fmt.Sprintf("i %v, items[i] == list.Get(i), %v, %v", i, item, lg), lg.Equals(item))
	}
}
func deserialize(bytes []byte) (*set.SortedSet, error) {
	marshal, unmarshal := types.IntMarshals()
	m := &set.MSortedSet{MSorted: list.MSorted{MList: list.MList{MarshalItem: marshal, UnmarshalItem: unmarshal}}}
	err := m.UnmarshalBinary(bytes)
	if err != nil {
		return nil, err
	}
	return m.SortedSet(), nil
}
func TestAddMarshalUnmarshalHas(x *testing.T) {
	t := (*T)(x)
	SIZE := 100
	set := NewSortedSet(10)
	items := make([]types.Int, 0, SIZE)
	for i := 0; i < SIZE; i++ {
		item := types.Int(rand.Intn(10) + 1)
		items = append(items, item)
		t.assert_nil(set.Add(item))
	}
	for i, item := range items {
		t.assert(fmt.Sprintf("!set.Has(item)", i), set.Has(item))
	}
	marshal, unmarshal := types.IntMarshals()
	mset1 := NewMSortedSet(set, marshal, unmarshal)
	bytes, err := mset1.MarshalBinary()
	t.assert_nil(err)
	mset2 := &MSortedSet{MSorted: list.MSorted{MList: list.MList{MarshalItem: marshal, UnmarshalItem: unmarshal}}}
	t.assert_nil(mset2.UnmarshalBinary(bytes))
	set2 := mset2.SortedSet()
	for i, item := range items {
		t.assert(fmt.Sprintf("!set.Has(item)", i), set2.Has(item))
	}
}
func serialize(s *set.SortedSet) ([]byte, error) {
	marshal, unmarshal := types.IntMarshals()
	m := set.NewMSortedSet(s, marshal, unmarshal)
	return m.MarshalBinary()
}