gorgonia.org/gorgonia@v0.9.17/internal/encoding/groups_test.go (about) 1 package encoding 2 3 import ( 4 "sort" 5 "testing" 6 ) 7 8 func TestGroups(t *testing.T) { 9 var g Groups 10 g1 := NewGroup("g1") 11 g2 := NewGroup("g2") 12 g = g.Upsert(g1) 13 if len(g) != 1 { 14 t.Fail() 15 } 16 g = g.Upsert(g1) 17 if len(g) != 1 { 18 t.Fail() 19 } 20 g = g.Upsert(g2) 21 if len(g) != 2 { 22 t.Fail() 23 } 24 if g[0].Name != "g1" && g[1].Name != "g2" { 25 t.Fail() 26 } 27 if !g.Have(g1) { 28 t.Fail() 29 } 30 if !g.Have(g2) { 31 t.Fail() 32 } 33 if g.Have(NewGroup("g2")) { 34 t.Fail() 35 } 36 37 } 38 39 func TestSorts(t *testing.T) { 40 var gs Groups 41 g1 := NewGroup("A") 42 g2 := NewGroup("B") 43 44 gs = gs.Upsert(g2) 45 gs = gs.Upsert(g1) 46 47 t.Logf("%v", gs) 48 sort.Sort(gs) 49 if gs[0] != g1 { 50 t.Errorf("Groups: Expected the first element to be Group A. g1's ID %d", g1.ID) 51 } 52 if gs[1] != g2 { 53 t.Errorf("Groups: Expected the second element to be Group B, g2's ID %d", g2.ID) 54 } 55 56 t.Logf("%v", gs) 57 58 // reset 59 60 gs = gs[:0] 61 gs = gs.Upsert(g2) 62 gs = gs.Upsert(g1) 63 64 // check that resets work 65 if gs[0] != g2 { 66 t.Fatal("Must not proceed. Reset failed") 67 } 68 69 sort.Sort(ByName(gs)) 70 if gs[0] != g1 { 71 t.Errorf("ByName: Expected the first element to be Group A. g1's name: %q", g1.Name) 72 } 73 if gs[1] != g2 { 74 t.Errorf("ByName: Expected the second element to be Group B. g2's name: %q", g2.Name) 75 } 76 77 }