blob: 376ac58ef9f99d84a4d3fe61bb5d2626912bc2ea [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Jiri Simsa5293dcb2014-05-10 09:56:38 -07005package vif
6
7import (
8 "reflect"
9 "testing"
10
Matt Rosencrantz94502cf2015-03-18 09:43:44 -070011 "v.io/x/ref/profiles/internal/rpc/stream/vc"
Jiri Simsa5293dcb2014-05-10 09:56:38 -070012)
13
14func TestVCMap(t *testing.T) {
15 m := newVCMap()
16
17 vc12 := vc.InternalNew(vc.Params{VCI: 12})
18 vc34 := vc.InternalNew(vc.Params{VCI: 34})
19 vc45 := vc.InternalNew(vc.Params{VCI: 45})
20
21 if vc, _, _ := m.Find(12); vc != nil {
22 t.Errorf("Unexpected VC found: %+v", vc)
23 }
24 if ok, _, _ := m.Insert(vc34); !ok {
25 t.Errorf("Insert should have returned true on first insert")
26 }
27 if ok, _, _ := m.Insert(vc34); ok {
28 t.Errorf("Insert should have returned false on second insert")
29 }
30 if ok, _, _ := m.Insert(vc12); !ok {
31 t.Errorf("Insert should have returned true on first insert")
32 }
33 if ok, _, _ := m.Insert(vc45); !ok {
34 t.Errorf("Insert should have returned true on the first insert")
35 }
36 if g, w := m.List(), []*vc.VC{vc12, vc34, vc45}; !reflect.DeepEqual(g, w) {
37 t.Errorf("Did not get all VCs in expected order. Got %v, want %v", g, w)
38 }
39 m.Delete(vc34.VCI())
40 if g, w := m.List(), []*vc.VC{vc12, vc45}; !reflect.DeepEqual(g, w) {
41 t.Errorf("Did not get all VCs in expected order. Got %v, want %v", g, w)
42 }
43}
44
45func TestVCMapFreeze(t *testing.T) {
46 m := newVCMap()
47 vc1 := vc.InternalNew(vc.Params{VCI: 1})
48 vc2 := vc.InternalNew(vc.Params{VCI: 2})
49 if ok, _, _ := m.Insert(vc1); !ok {
50 t.Fatal("Should be able to insert the VC")
51 }
52 m.Freeze()
53 if ok, _, _ := m.Insert(vc2); ok {
54 t.Errorf("Should not be able to insert a VC after Freeze")
55 }
56 if vc, _, _ := m.Find(1); vc != vc1 {
57 t.Errorf("Got %v want %v", vc, vc1)
58 }
59 m.Delete(vc1.VCI())
60 if vc, _, _ := m.Find(1); vc != nil {
61 t.Errorf("Got %v want nil", vc)
62 }
63}
Jungho Ahncd175b82015-03-27 14:29:40 -070064
65func TestVCMapDelete(t *testing.T) {
66 m := newVCMap()
67
68 vc1 := vc.InternalNew(vc.Params{VCI: 1})
69 vc2 := vc.InternalNew(vc.Params{VCI: 2})
70
71 m.Insert(vc1)
72 if empty := m.Delete(vc1.VCI()); !empty {
73 t.Error("Want empty; got false")
74 }
75
76 m.Insert(vc1)
77 m.Insert(vc2)
78
79 m.Delete(vc1.VCI())
80 if empty := m.Delete(vc1.VCI()); empty {
81 t.Error("Want not empty; got true")
82 }
83 if empty := m.Delete(vc2.VCI()); !empty {
84 t.Error("Want empty; got false")
85 }
86}