예제 #1
0
func TestMapSetIterators(x *testing.T) {
	t := (*T)(x)
	SIZE := 100
	set := NewMapSet(NewSortedSet(10))
	for i := 0; i < SIZE; i++ {
		item := &types.MapEntry{types.Int(rand.Intn(1000)), types.Int(rand.Intn(1000))}
		t.assert_nil(set.Put(item.Key, item.Value))
	}
	item, items := set.Items()()
	key, keys := set.Keys()()
	for items != nil && keys != nil {
		if !item.(*types.MapEntry).Key.Equals(key) {
			t.Fatal("item != key")
		}
		item, items = items()
		key, keys = keys()
	}
	t.assert("uneven iteration", items == nil && keys == nil)
	item, items = set.Items()()
	val, vals := set.Values()()
	for items != nil && vals != nil {
		if !item.(*types.MapEntry).Value.(types.Int).Equals(val.(types.Int)) {
			t.Fatal("item != key")
		}
		item, items = items()
		val, vals = vals()
	}
	t.assert("uneven iteration", items == nil && keys == nil)
}
예제 #2
0
func TestAddHasDeleteRandom(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(1000))
		for set.Has(item) {
			item = types.Int(rand.Intn(1000))
		}
		items = append(items, item)
		t.assert_nil(set.Add(item))
	}
	for i, item := range items {
		t.assert(fmt.Sprintf("i %v, !set.Has(item)", i), set.Has(item))
	}
	for i := 0; i < 10; i++ {
		item, err := set.Random()
		t.assert_nil(err)
		t.assert(fmt.Sprintf("!set.Has(item)", i), set.Has(item))
	}
	for _, item := range items {
		t.assert_nil(set.Delete(item))
	}
	for i, item := range items {
		t.assert(fmt.Sprintf("i %v, !set.Has(item)", i), !set.Has(item))
	}
	_, err := set.Random()
	t.assert(fmt.Sprintf("err == nil"), err != nil)
	t.assert_nil(set.Add(types.Int(1)))
	item, err := set.Random()
	t.assert_nil(err)
	t.assert(fmt.Sprintf("item == 1"), item.Equals(types.Int(1)))
}
예제 #3
0
func Test_put_no_root_split(t *testing.T) {
	a := NewLeaf(2, false)
	if err := a.put_kv(types.Int(1), 1); err != nil {
		t.Error(err)
	}
	p, err := a.put(types.Int(1), 2)
	if err != nil {
		t.Error(err)
	} else {
		if p != a {
			t.Errorf("p != a")
		}
		if !p.has(types.Int(1)) {
			t.Error("p didn't have the right keys", p)
		}
	}
	p, err = a.put(types.Int(1), 3)
	if err != nil {
		t.Error(err)
	} else {
		if p != a {
			t.Errorf("p != a")
		}
		if !p.has(types.Int(1)) {
			t.Error("p didn't have the right keys", p)
		}
		if p.next == nil {
			t.Error("p.next should not be nil")
		}
		t.Log(p)
		t.Log(p.next)
	}
}
예제 #4
0
func TestMapSetAddHasDeleteRandom(x *testing.T) {
	t := (*T)(x)
	SIZE := 100
	a := NewMapSet(NewSortedSet(10))
	items := make([]*types.MapEntry, 0, SIZE)
	for i := 0; i < SIZE; i++ {
		item := &types.MapEntry{types.Int(rand.Intn(1000)), types.Int(rand.Intn(1000))}
		for a.Has(item) {
			item = &types.MapEntry{types.Int(rand.Intn(1000)), types.Int(rand.Intn(1000))}
		}
		items = append(items, item)
		t.assert_nil(a.Add(item))
	}
	set := NewMapSet(NewSortedSet(10))
	t.assert_nil(set.Extend(a.Items()))
	t.assert("set == a", set.Equals(a))
	for i, item := range items {
		thing, err := set.Item(item)
		t.assert_nil(err)
		t.assert(fmt.Sprintf("i %v, thing == item", i), thing.Equals(item))
		t.assert(fmt.Sprintf("i %v, !set.Has(item)", i), set.Has(item))
	}
	for _, item := range items {
		t.assert_nil(set.Delete(item))
	}
	for i, item := range items {
		t.assert(fmt.Sprintf("i %v, !set.Has(item)", i), !set.Has(item))
	}
}
예제 #5
0
파일: graph.go 프로젝트: timtadh/graple
func LoadEdge(g *goiso.Graph, vids types.Map, data []byte) (err error) {
	obj, err := ParseJson(data)
	if err != nil {
		return err
	}
	_src, err := obj["src"].(json.Number).Int64()
	if err != nil {
		return err
	}
	_targ, err := obj["targ"].(json.Number).Int64()
	if err != nil {
		return err
	}
	src := int(_src)
	targ := int(_targ)
	label := strings.TrimSpace(obj["label"].(string))
	if o, err := vids.Get(types.Int(src)); err != nil {
		return err
	} else {
		u := o.(*goiso.Vertex)
		if o, err := vids.Get(types.Int(targ)); err != nil {
			return err
		} else {
			v := o.(*goiso.Vertex)
			g.AddEdge(u, v, label)
		}
	}
	return nil
}
예제 #6
0
func TestFixedSorted(x *testing.T) {
	t := (*T)(x)
	SIZE := 100
	set := NewFixedSorted(SIZE, false)
	items := make([]types.Int, 0, SIZE)
	for i := 0; i < SIZE; i++ {
		item := types.Int(rand.Intn(1000))
		for set.Has(item) {
			item = types.Int(rand.Intn(1000))
		}
		items = append(items, item)
		t.assert_nil(set.Add(item))
	}
	t.assert(fmt.Sprintf("set.Full() (%v)", set.Full()), set.Full())
	t.assert(fmt.Sprintf("set.Size == %d (%d)", SIZE, set.Size()), set.Size() == SIZE)
	for i, item := range items {
		t.assert(fmt.Sprintf("i %v, !set.Has(item)", i), set.Has(item))
	}
	for _, item := range items {
		t.assert_nil(set.Delete(item))
	}
	for i, item := range items {
		t.assert(fmt.Sprintf("i %v, !set.Has(item)", i), !set.Has(item))
	}
}
예제 #7
0
func TestMapSetMapFunc(x *testing.T) {
	t := (*T)(x)
	SIZE := 100
	set := types.Map(NewMapSet(NewSortedSet(10)))
	items := make([]*types.MapEntry, 0, SIZE)
	for i := 0; i < SIZE; i++ {
		item := &types.MapEntry{types.Int(rand.Intn(1000)), types.Int(rand.Intn(1000))}
		for set.Has(item.Key) {
			item = &types.MapEntry{types.Int(rand.Intn(1000)), types.Int(rand.Intn(1000))}
		}
		items = append(items, item)
		t.assert_nil(set.Put(item.Key, item.Value))
	}
	for i, item := range items {
		t.assert(fmt.Sprintf("i %v, !set.Has(item, %v)", i, item), set.Has(item.Key))
	}
	t.assert("size == 100", set.Size() == 100)
	for _, item := range items {
		rm, err := set.Remove(item.Key)
		t.assert_nil(err)
		t.assert("item == rm", item.Value.(types.Int).Equals(rm.(types.Int)))
	}
	for i, item := range items {
		t.assert(fmt.Sprintf("i %v, set.Has(item)", i), !set.Has(item.Key))
	}
}
예제 #8
0
func TestSubtract(x *testing.T) {
	t := (*T)(x)
	a := FromSlice([]types.Hashable{types.Int(0), types.Int(1), types.Int(2), types.Int(3)})
	b := FromSlice([]types.Hashable{types.Int(1), types.Int(2), types.Int(4)})
	c := FromSlice([]types.Hashable{types.Int(0), types.Int(3)})
	d := FromSlice([]types.Hashable{types.Int(4)})
	t.assert("a - b == c", t.assert_set(a.Subtract(b)).Equals(c))
	t.assert("b - a == d", t.assert_set(b.Subtract(a)).Equals(d))
}
예제 #9
0
func Test_leaf_split_equal(t *testing.T) {
	a := NewLeaf(3, false)
	insert_linked_list_node(a, nil, nil)
	if err := a.put_kv(types.Int(1), 1); err != nil {
		t.Error(err)
	}
	if err := a.put_kv(types.Int(3), 3); err != nil {
		t.Error(err)
	}
	if err := a.put_kv(types.Int(5), 5); err != nil {
		t.Error(err)
	}
	p, q, err := a.leaf_split(types.Int(3), 2)
	if err != nil {
		t.Error(err)
	} else {
		if p != a {
			t.Errorf("p != a")
		}
		if q == nil {
			t.Errorf("q == nil")
		}
		if !p.has(types.Int(1)) {
			t.Error("p didn't have the right keys", p)
		}
		if !q.has(types.Int(3)) || q.count(types.Int(3)) != 2 || !q.has(types.Int(5)) {
			t.Error("q didn't have the right keys", q, q.count(types.Int(3)))
		}
	}
}
예제 #10
0
func Test_internal_insert_split_less(t *testing.T) {
	a := NewInternal(3)
	leaf := NewLeaf(1, false)
	if err := leaf.put_kv(types.Int(1), 1); err != nil {
		t.Error(err)
	}
	if err := a.put_kp(types.Int(1), leaf); err != nil {
		t.Error(err)
	}
	if err := a.put_kp(types.Int(3), nil); err != nil {
		t.Error(err)
	}
	if err := a.put_kp(types.Int(5), nil); err != nil {
		t.Error(err)
	}
	p, q, err := a.internal_insert(types.Int(2), nil)
	if err != nil {
		t.Error(err)
	} else {
		if p != a {
			t.Errorf("p != a")
		}
		if q == nil {
			t.Errorf("q == nil")
		}
		if !p.has(types.Int(1)) || !p.has(types.Int(2)) {
			t.Error("p didn't have the right keys", p)
		}
		if !q.has(types.Int(3)) || !q.has(types.Int(5)) {
			t.Error("q didn't have the right keys", q)
		}
	}
}
예제 #11
0
파일: dbscan.go 프로젝트: timtadh/sfp
func digraphLabelset(n *digraph.EmbListNode) (types.Set, error) {
	p := n.Pat
	s := set.NewSortedSet(len(p.V) + len(p.E))
	for i := range p.V {
		s.Add(types.Int(p.V[i].Color))
	}
	for i := range p.E {
		s.Add(types.Int(p.E[i].Color))
	}
	return s, nil
}
예제 #12
0
파일: extensions.go 프로젝트: timtadh/sfp
func extensionsFromEmbeddings(dt *Digraph, pattern *subgraph.SubGraph, ei subgraph.EmbIterator, seen map[int]bool) (total int, overlap []map[int]bool, fisEmbs []*subgraph.Embedding, sets []*hashtable.LinearHash, exts types.Set) {
	if dt.Mode&FIS == FIS {
		seen = make(map[int]bool)
		fisEmbs = make([]*subgraph.Embedding, 0, 10)
	} else {
		sets = make([]*hashtable.LinearHash, len(pattern.V))
	}
	if dt.Mode&OverlapPruning == OverlapPruning {
		overlap = make([]map[int]bool, len(pattern.V))
	}
	exts = set.NewSetMap(hashtable.NewLinearHash())
	add := validExtChecker(dt, func(emb *subgraph.Embedding, ext *subgraph.Extension) {
		exts.Add(ext)
	})
	for emb, next := ei(false); next != nil; emb, next = next(false) {
		seenIt := false
		for idx, id := range emb.Ids {
			if fisEmbs != nil {
				if seen[id] {
					seenIt = true
				}
			}
			if overlap != nil {
				if overlap[idx] == nil {
					overlap[idx] = make(map[int]bool)
				}
				overlap[idx][id] = true
			}
			if seen != nil {
				seen[id] = true
			}
			if sets != nil {
				if sets[idx] == nil {
					sets[idx] = hashtable.NewLinearHash()
				}
				set := sets[idx]
				if !set.Has(types.Int(id)) {
					set.Put(types.Int(id), emb)
				}
			}
			for _, e := range dt.G.Kids[id] {
				add(emb, &dt.G.E[e], idx, -1)
			}
			for _, e := range dt.G.Parents[id] {
				add(emb, &dt.G.E[e], -1, idx)
			}
		}
		if fisEmbs != nil && !seenIt {
			fisEmbs = append(fisEmbs, emb)
		}
		total++
	}
	return total, overlap, fisEmbs, sets, exts
}
예제 #13
0
func Test_internal_split_greater(t *testing.T) {
	a := NewInternal(3)
	if err := a.put_kp(types.Int(1), nil); err != nil {
		t.Error(err)
	}
	if err := a.put_kp(types.Int(3), nil); err != nil {
		t.Error(err)
	}
	if err := a.put_kp(types.Int(5), nil); err != nil {
		t.Error(err)
	}
	p, q, err := a.internal_split(types.Int(4), nil)
	if err != nil {
		t.Error(err)
	} else {
		if p != a {
			t.Errorf("p != a")
		}
		if q == nil {
			t.Errorf("q == nil")
		}
		if !p.has(types.Int(1)) {
			t.Error("p didn't have the right keys", p)
		}
		if !q.has(types.Int(3)) || !q.has(types.Int(4)) || !q.has(types.Int(5)) {
			t.Error("q didn't have the right keys", q)
		}
	}
}
예제 #14
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))
	}
}
예제 #15
0
파일: graph.go 프로젝트: timtadh/graple
func LoadVertex(g *goiso.Graph, supportAttr string, vids types.Map, nodeAttrs *bptree.BpTree, supportAttrs map[int]string, data []byte) (err error) {
	obj, err := ParseJson(data)
	if err != nil {
		return err
	}
	_id, err := obj["id"].(json.Number).Int64()
	if err != nil {
		return err
	}
	label := strings.TrimSpace(obj["label"].(string))
	id := int(_id)
	vertex := g.AddVertex(id, label)
	err = vids.Put(types.Int(id), vertex)
	if err != nil {
		return err
	}
	if nodeAttrs != nil {
		bid := make([]byte, 4)
		binary.BigEndian.PutUint32(bid, uint32(vertex.Idx))
		err = nodeAttrs.Add(bid, data)
		if err != nil {
			return err
		}
		if supportAttr != "" {
			if _, has := obj[supportAttr]; !has {
				return fmt.Errorf("vertex did not have required supportAttr %v\n%v", supportAttr, string(data))
			}
			supportAttrs[vertex.Idx] = obj[supportAttr].(string)
		}
	}
	return nil
}
예제 #16
0
파일: embeddings.go 프로젝트: timtadh/sfp
func FilterAutomorphs(it EmbIterator, dropped *VertexEmbeddings) (ei EmbIterator, _ *VertexEmbeddings) {
	idSet := func(emb *Embedding) *list.Sorted {
		ids := list.NewSorted(len(emb.Ids), true)
		for _, id := range emb.Ids {
			ids.Add(types.Int(id))
		}
		return ids
	}
	seen := hashtable.NewLinearHash()
	ei = func(stop bool) (emb *Embedding, _ EmbIterator) {
		if it == nil {
			return nil, nil
		}
		for emb, it = it(stop); it != nil; emb, it = it(stop) {
			ids := idSet(emb)
			// errors.Logf("AUTOMORPH-DEBUG", "emb %v ids %v has %v", emb, ids, seen.Has(ids))
			if !seen.Has(ids) {
				seen.Put(ids, nil)
				return emb, ei
			}
		}
		return nil, nil
	}
	return ei, dropped
}
예제 #17
0
파일: embeddings.go 프로젝트: timtadh/sfp
func (ids *IdNode) idSet(length int) *set.SortedSet {
	s := set.NewSortedSet(length)
	for c := ids; c != nil; c = c.Prev {
		s.Add(types.Int(c.Id))
	}
	return s
}
예제 #18
0
func Test_internal_split_equal(t *testing.T) {
	a := NewInternal(3)
	if err := a.put_kp(types.Int(1), nil); err != nil {
		t.Error(err)
	}
	if err := a.put_kp(types.Int(3), nil); err != nil {
		t.Error(err)
	}
	if err := a.put_kp(types.Int(5), nil); err != nil {
		t.Error(err)
	}
	p, q, err := a.internal_split(types.Int(3), nil)
	if err == nil {
		t.Error("split succeeded should have failed", p, q)
	}
}
예제 #19
0
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))
	}
}
예제 #20
0
파일: dbscan.go 프로젝트: timtadh/sfp
func digraphItemset(n *digraph.EmbListNode, attr string) (types.Set, error) {
	dt := n.Dt
	embs, err := n.Embeddings()
	if err != nil {
		return nil, err
	}
	s := set.NewSortedSet(len(embs))
	for _, emb := range embs {
		for _, vid := range emb.Ids {
			err := dt.NodeAttrs.DoFind(
				int32(vid),
				func(_ int32, attrs map[string]interface{}) error {
					if val, has := attrs[attr]; has {
						switch v := val.(type) {
						case string:
							s.Add(types.String(v))
						case int:
							s.Add(types.Int(v))
						default:
							return errors.Errorf("DBSCAN does not yet support attr type %T", val)
						}
					}
					return nil
				})
			if err != nil {
				return nil, err
			}
		}
	}
	return s, nil
}
예제 #21
0
func TestIterators(t *testing.T) {
    var data []int = []int{
        1, 5, 7, 9, 12, 13, 17, 18, 19, 20,
    }
    var order []int = []int{
        6, 1, 8, 2, 4 , 9 , 5 , 7 , 0 , 3 ,
    }

    test := func(tree types.TreeMap) {
        t.Logf("%T", tree)
        for j := range order {
            if err := tree.Put(types.Int(data[order[j]]), order[j]); err != nil {
                t.Error(err)
            }
        }

        j := 0
        for k, v, next := tree.Iterate()(); next != nil; k, v, next = next() {
            if !k.Equals(types.Int(data[j])) {
                t.Error("Wrong key")
            }
            if v.(int) != j {
                t.Error("Wrong value")
            }
            j += 1
        }

        j = 0
        for k, next := tree.Keys()(); next != nil; k, next = next() {
            if !k.Equals(types.Int(data[j])) {
                t.Error("Wrong key")
            }
            j += 1
        }

        j = 0
        for v, next := tree.Values()(); next != nil; v, next = next() {
            if v.(int) != j {
                t.Error("Wrong value")
            }
            j += 1
        }
    }
    test(NewAvlTree())
    test(NewImmutableAvlTree())
}
예제 #22
0
func Test_find_end_of_pure_run(t *testing.T) {
	a := NewLeaf(2, false)
	insert_linked_list_node(a, nil, nil)
	b := NewLeaf(2, false)
	insert_linked_list_node(b, a, nil)
	c := NewLeaf(2, false)
	insert_linked_list_node(c, b, nil)
	d := NewLeaf(2, false)
	insert_linked_list_node(d, c, nil)
	if err := a.put_kv(types.Int(3), 1); err != nil {
		t.Error(err)
	}
	if err := a.put_kv(types.Int(3), 2); err != nil {
		t.Error(err)
	}
	if err := b.put_kv(types.Int(3), 3); err != nil {
		t.Error(err)
	}
	if err := b.put_kv(types.Int(3), 4); err != nil {
		t.Error(err)
	}
	if err := c.put_kv(types.Int(3), 5); err != nil {
		t.Error(err)
	}
	if err := c.put_kv(types.Int(3), 6); err != nil {
		t.Error(err)
	}
	if err := d.put_kv(types.Int(4), 6); err != nil {
		t.Error(err)
	}
	e := a.find_end_of_pure_run()
	if e != c {
		t.Errorf("end of run should have been block c %v %v", e, c)
	}
}
예제 #23
0
파일: support.go 프로젝트: timtadh/graple
func VertexSet(sg *goiso.SubGraph) *set.SortedSet {
	s := set.NewSortedSet(len(sg.V))
	for _, v := range sg.V {
		if err := s.Add(types.Int(v.Id)); err != nil {
			panic(err)
		}
	}
	return s
}
예제 #24
0
func (st *SurveyLogStore) loadLine(line []byte, answers *[]*models.SurveyAnswer, answered *set.SortedSet) error {
	var a models.SurveyAnswer
	err := json.Unmarshal(line, &a)
	if err != nil {
		return err
	}
	answered.Add(types.Int(a.CloneID))
	*answers = append(*answers, &a)
	return nil
}
예제 #25
0
파일: queue.go 프로젝트: jmptrader/queued-1
/* Put data on the queue */
func (self *Queue) Enque(data []byte) error {
	self.lock.Lock()
	defer self.lock.Unlock()

	h := types.ByteSlice(Hash(data))
	has := self.index.Has(h)
	if !self.allowDups && has {
		return nil
	} else if has {
		i, err := self.index.Get(h)
		if err != nil {
			return err
		}
		err = self.index.Put(h, types.Int(int(i.(types.Int))+1))
		if err != nil {
			return err
		}
	} else {
		err := self.index.Put(h, types.Int(1))
		if err != nil {
			return err
		}
	}

	node := &node{next: nil, data: data}

	if self.tail == nil {
		if self.head != nil {
			return fmt.Errorf("List has head but no tail...")
		}
		self.head = node
		self.tail = node
	} else {
		self.tail.next = node
		self.tail = node
	}
	self.length += 1

	return nil
}
예제 #26
0
func Test_balance_internal_nodes(t *testing.T) {
	a := NewInternal(6)
	b := NewInternal(6)
	if err := a.put_kp(types.Int(1), nil); err != nil {
		t.Error(err)
	}
	if err := a.put_kp(types.Int(2), nil); err != nil {
		t.Error(err)
	}
	if err := a.put_kp(types.Int(3), nil); err != nil {
		t.Error(err)
	}
	if err := a.put_kp(types.Int(4), nil); err != nil {
		t.Error(err)
	}
	if err := a.put_kp(types.Int(5), nil); err != nil {
		t.Error(err)
	}
	if err := a.put_kp(types.Int(6), nil); err != nil {
		t.Error(err)
	}
	balance_nodes(a, b)
	for i, k := range a.keys {
		if int(k.(types.Int)) != i+1 {
			t.Errorf("k != %d", i+1)
		}
	}
	for i, k := range b.keys {
		if int(k.(types.Int)) != 3+i+1 {
			t.Errorf("k != %d", 3+i+1)
		}
	}
	t.Log(a)
	t.Log(b)
}
예제 #27
0
파일: queue.go 프로젝트: jmptrader/queued-1
/* Read data off the queue in FIFO order */
func (self *Queue) Deque() (data []byte, err error) {
	self.lock.Lock()
	defer self.lock.Unlock()

	if self.length == 0 {
		return nil, fmt.Errorf("List is empty")
	}
	if self.length < 0 {
		return nil, fmt.Errorf("List length is less than zero")
	}
	if self.head == nil {
		return nil, fmt.Errorf("head is nil")
	}

	node := self.head

	if node.next == nil {
		if node != self.tail {
			return nil, fmt.Errorf("Expected tail to equal head")
		}
		if self.length != 1 {
			return nil, fmt.Errorf("Expected list length to equal 1")
		}
		self.tail = nil
	}
	self.head = node.next
	self.length -= 1

	h := types.ByteSlice(Hash(node.data))
	if self.index.Has(h) {
		i, err := self.index.Get(h)
		if err != nil {
			return nil, err
		}
		j := int(i.(types.Int)) - 1
		if j <= 0 {
			_, err = self.index.Remove(h)
			if err != nil {
				return nil, err
			}
		} else {
			err = self.index.Put(h, types.Int(j))
			if err != nil {
				return nil, err
			}
		}
	} else {
		return nil, fmt.Errorf("integrity error, index did not have data")
	}

	return node.data, nil
}
예제 #28
0
func TestAddHasDelete(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(1000))
		for set.Has(item) {
			item = types.Int(rand.Intn(1000))
		}
		items = append(items, item)
		t.assert_nil(set.Add(item))
	}
	for i, item := range items {
		t.assert(fmt.Sprintf("i %v, !set.Has(item)", i), set.Has(item))
	}
	for _, item := range items {
		t.assert_nil(set.Delete(item))
	}
	for i, item := range items {
		t.assert(fmt.Sprintf("i %v, !set.Has(item)", i), !set.Has(item))
	}
}
예제 #29
0
func TestSubsetSuperset(x *testing.T) {
	t := (*T)(x)
	a := FromSlice([]types.Hashable{types.Int(0), types.Int(1), types.Int(2), types.Int(3)})
	b := FromSlice([]types.Hashable{types.Int(1), types.Int(2), types.Int(4)})
	c := FromSlice([]types.Hashable{types.Int(1), types.Int(2)})
	t.assert("a not subset b", !a.Subset(b))
	t.assert("b not subset a", !b.Subset(a))
	t.assert("c subset a", c.Subset(a))
	t.assert("c subset b", c.Subset(b))
	t.assert("c proper subset a", c.ProperSubset(a))
	t.assert("c proper subset b", c.ProperSubset(b))
	t.assert("a subset a", a.Subset(a))
	t.assert("a not proper subset a", !a.ProperSubset(a))
	t.assert("a superset a", a.Superset(a))
	t.assert("a not proper superset a", !a.ProperSuperset(a))
	t.assert("a superset c", a.Superset(c))
	t.assert("b superset c", b.Superset(c))
	t.assert("a superset c", a.ProperSuperset(c))
	t.assert("b superset c", b.ProperSuperset(c))
}
예제 #30
0
func TestTraversals(t *testing.T) {
	var data []int = []int{
		1, 5, 7, 9, 12, 13, 17, 18, 19, 20,
	}
	var order []int = []int{
		6, 1, 8, 2, 4, 9, 5, 7, 0, 3,
	}
	var preorder []int = []int{
		17, 7, 5, 1, 12, 9, 13, 19, 18, 20,
	}
	var postorder []int = []int{
		1, 5, 9, 13, 12, 7, 18, 20, 19, 17,
	}

	test := func(T types.TreeMap) {
		t.Logf("%T", T)
		for j := range order {
			if err := T.Put(types.Int(data[order[j]]), order[j]); err != nil {
				t.Error(err)
			}
		}

		j := 0
		for tn, next := tree.TraverseBinaryTreeInOrder(T.Root().(types.BinaryTreeNode))(); next != nil; tn, next = next() {
			if int(tn.Key().(types.Int)) != data[j] {
				t.Error("key in wrong spot in-order")
			}
			j += 1
		}

		j = 0
		for tn, next := tree.TraverseTreePreOrder(T.Root())(); next != nil; tn, next = next() {
			if int(tn.Key().(types.Int)) != preorder[j] {
				t.Error("key in wrong spot pre-order")
			}
			j += 1
		}

		j = 0
		for tn, next := tree.TraverseTreePostOrder(T.Root())(); next != nil; tn, next = next() {
			if int(tn.Key().(types.Int)) != postorder[j] {
				t.Error("key in wrong spot post-order")
			}
			j += 1
		}
	}
	test(NewAvlTree())
	test(NewImmutableAvlTree())
}