algorithms/fundamentals/bag/bag_test.go

37 lines
477 B
Go
Raw Normal View History

2021-11-08 21:53:15 +02:00
package bag
import (
"log"
"testing"
)
func TestSimple(t *testing.T) {
2021-12-15 23:40:14 +02:00
bag := NewBag[int]()
2021-11-08 21:53:15 +02:00
sum := 0
bag.Add(1)
bag.Add(2)
bag.Add(3)
2021-12-15 23:40:14 +02:00
bag.ForEach(func(i int) { sum += i })
2021-11-08 21:53:15 +02:00
if sum != 6 {
log.Fatal("wrong items in bag")
}
}
func TestEmpty(t *testing.T) {
2021-12-15 23:40:14 +02:00
bag := NewBag[int]()
2021-11-08 21:53:15 +02:00
sum := 0
if bag.Size() != 0 || !bag.IsEmpty() {
log.Fatal("bag should be empty")
}
2021-12-15 23:40:14 +02:00
bag.ForEach(func(i int) { sum += i })
2021-11-08 21:53:15 +02:00
if sum != 0 {
log.Fatal("wrong items in bag")
}
}