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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
package set
import (
"reflect"
"testing"
)
func Test_Union(t *testing.T) {
s := newTS()
s.Add("1", "2", "3")
r := newTS()
r.Add("3", "4", "5")
x := newNonTS()
x.Add("5", "6", "7")
u := Union(s, r, x)
if settype := reflect.TypeOf(u).String(); settype != "*set.Set" {
t.Error("Union should derive its set type from the first passed set, got", settype)
}
if u.Size() != 7 {
t.Error("Union: the merged set doesn't have all items in it.")
}
if !u.Has("1", "2", "3", "4", "5", "6", "7") {
t.Error("Union: merged items are not availabile in the set.")
}
z := Union(x, r)
if z.Size() != 5 {
t.Error("Union: Union of 2 sets doesn't have the proper number of items.")
}
if settype := reflect.TypeOf(z).String(); settype != "*set.SetNonTS" {
t.Error("Union should derive its set type from the first passed set, got", settype)
}
}
func Test_Difference(t *testing.T) {
s := newTS()
s.Add("1", "2", "3")
r := newTS()
r.Add("3", "4", "5")
x := newNonTS()
x.Add("5", "6", "7")
u := Difference(s, r, x)
if u.Size() != 2 {
t.Error("Difference: the set doesn't have all items in it.")
}
if !u.Has("1", "2") {
t.Error("Difference: items are not availabile in the set.")
}
y := Difference(r, r)
if y.Size() != 0 {
t.Error("Difference: size should be zero")
}
}
func Test_Intersection(t *testing.T) {
s1 := newTS()
s1.Add("1", "3", "4", "5")
s2 := newTS()
s2.Add("3", "5", "6")
s3 := newTS()
s3.Add("4", "5", "6", "7")
u := Intersection(s1, s2, s3)
if u.Size() != 1 {
t.Error("Intersection: the set doesn't have all items in it.")
}
if !u.Has("5") {
t.Error("Intersection: items after intersection are not availabile in the set.")
}
}
func Test_Intersection2(t *testing.T) {
s1 := newTS()
s1.Add("1", "3", "4", "5")
s2 := newTS()
s2.Add("5", "6")
i := Intersection(s1, s2)
if i.Size() != 1 {
t.Error("Intersection: size should be 1, it was", i.Size())
}
if !i.Has("5") {
t.Error("Intersection: items after intersection are not availabile in the set.")
}
}
func Test_SymmetricDifference(t *testing.T) {
s := newTS()
s.Add("1", "2", "3")
r := newTS()
r.Add("3", "4", "5")
u := SymmetricDifference(s, r)
if u.Size() != 4 {
t.Error("SymmetricDifference: the set doesn't have all items in it.")
}
if !u.Has("1", "2", "4", "5") {
t.Error("SymmetricDifference: items are not availabile in the set.")
}
}
func Test_StringSlice(t *testing.T) {
s := newTS()
s.Add("san francisco", "istanbul", 3.14, 1321, "ankara")
u := StringSlice(s)
if len(u) != 3 {
t.Error("StringSlice: slice should only have three items")
}
for _, item := range u {
r := reflect.TypeOf(item)
if r.Kind().String() != "string" {
t.Error("StringSlice: slice item should be a string")
}
}
}
func Test_IntSlice(t *testing.T) {
s := newTS()
s.Add("san francisco", "istanbul", 3.14, 1321, "ankara", 8876)
u := IntSlice(s)
if len(u) != 2 {
t.Error("IntSlice: slice should only have two items")
}
for _, item := range u {
r := reflect.TypeOf(item)
if r.Kind().String() != "int" {
t.Error("Intslice: slice item should be a int")
}
}
}
func BenchmarkSetEquality(b *testing.B) {
s := newTS()
u := newTS()
for i := 0; i < b.N; i++ {
s.Add(i)
u.Add(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.IsEqual(u)
}
}
func BenchmarkSubset(b *testing.B) {
s := newTS()
u := newTS()
for i := 0; i < b.N; i++ {
s.Add(i)
u.Add(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.IsSubset(u)
}
}
func benchmarkIntersection(b *testing.B, numberOfItems int) {
s1 := newTS()
s2 := newTS()
for i := 0; i < numberOfItems/2; i++ {
s1.Add(i)
}
for i := 0; i < numberOfItems; i++ {
s2.Add(i)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
Intersection(s1, s2)
}
}
func BenchmarkIntersection10(b *testing.B) {
benchmarkIntersection(b, 10)
}
func BenchmarkIntersection100(b *testing.B) {
benchmarkIntersection(b, 100)
}
func BenchmarkIntersection1000(b *testing.B) {
benchmarkIntersection(b, 1000)
}
func BenchmarkIntersection10000(b *testing.B) {
benchmarkIntersection(b, 10000)
}
func BenchmarkIntersection100000(b *testing.B) {
benchmarkIntersection(b, 100000)
}
func BenchmarkIntersection1000000(b *testing.B) {
benchmarkIntersection(b, 1000000)
}
|