Esempio n. 1
0
func TestUtilityBinaryRelation(t *testing.T) {
	s := set.New()

	s.Add("one")
	s.Add("two")
	s.Add("three")

	ub := NewUtilityBinaryRelationOn(s, u)

	if !relation.Reflexive(ub) {
		t.Errorf("Our UtilityBinaryRelation should be reflexive")
	}

	if !relation.Complete(ub) {
		t.Errorf("Our UtilityBinaryRelation should be complete")
	}

	if !relation.Transitive(ub) {
		t.Errorf("Our UtilityBinaryRelation should be transitive")
	}

	if !Rational(Preference(ub)) {
		t.Errorf("Our UtilityBinaryRelation should be rational! -- von Neumann-Morgenstern")
	}
}
Esempio n. 2
0
// NewDiscreteDistribution constructs a discrete distribution over
// the set d, provided as the domain of the distribution
func NewDiscreteDistribution(d set.Interface) DiscreteDistribution {
	return &distribution{
		domain:   d,
		outcomes: set.New(),
		support:  make(map[Outcome]Probability),
	}
}
Esempio n. 3
0
func TestMembership(t *testing.T) {
	t.Parallel()

	testElements := make(map[int]bool)

	for i := 0; i < 123; i++ {
		testElements[i] = true
	}

	s := set.New()

	for k := range testElements {
		if s.Contains(k) {
			log.Fatalf("Set should not contain: %d", k)
		}

		s.Add(k)
	}

	for k := range testElements {
		if !s.Contains(k) {
			log.Fatalf("Set should contain %d", k)
		}

		s.Remove(k)
	}

	for k := range testElements {
		if s.Contains(k) {
			log.Fatal("Set should not contain %d", k)
		}
	}
}
Esempio n. 4
0
// Benchmarks the set implementation in package set
func BenchmarkSet(b *testing.B) {
	s := set.New()

	for n := 0; n < b.N; n++ {
		ok := s.Contains(n)
		if !ok {
			s.Add(n)
		}
	}
}
Esempio n. 5
0
func main() {
	s := set.New()

	s.Add(1)
	s.Add(2)
	s.Add(3)

	log.Printf("%s", s)

	ranks := set.With([]set.Element{"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"})
	suits := set.With([]set.Element{"♠", "♥", "♦", "♣"})
	deck := set.CartesianProduct(ranks, suits)

	log.Printf("Deck: %s", deck)
	log.Printf("Number of Cards: %d", deck.Cardinality())

	log.Printf("Union of ranks and suits, %v", set.Union(ranks, suits))

	log.Printf("Power set of {1, 2, 3}: %v", set.PowerSet(s))
}
Esempio n. 6
0
func TestSetBasicUsage(t *testing.T) {
	t.Parallel()

	s := set.New()

	s.Add(1)
	s.Add(2)
	s.Add(3)

	if !s.Contains(1) {
		t.Errorf("Expected set to contain 1")
	}

	if !s.Contains(2) {
		t.Errorf("Expected set to contain 2")
	}

	if !s.Contains(3) {
		t.Errorf("Expected set to contain 3")
	}

	if s.Contains(4) {
		t.Errorf("Expected set to not contain 4")
	}

	if s.Cardinality() != 3 {
		t.Errorf("Expected set cardinality to be 3")
	}

	s.Remove(2)

	if s.Contains(2) {
		t.Errorf("Expected set to no longer contain 2")
	}

	if len(s.Elements()) != int(s.Cardinality()) {
		log.Printf("Elements %+v", s.Elements())
		log.Printf("s: %+v", s)
		t.Errorf("Expected length of Elements() [%d] to match Cardinality() [%d]", len(s.Elements()), s.Cardinality())
	}
}
Esempio n. 7
0
func TestCardinality(t *testing.T) {
	t.Parallel()

	if set.Empty.Cardinality() != 0 {
		log.Fatal("Empty set should have cardinality 0")
	}

	s := set.New()

	for i := 0; i < 100; i++ {
		s.Add(i)
	}

	if s.Cardinality() != 100 {
		log.Fatal("S should have cardinality 100, but has cardinality %d", s.Cardinality())
	}

	elements := s.Elements()

	if len(elements) != 100 {
		log.Fatal("S should return an []Element of length 100")
	}

	elements[46] = nil

	if !s.Contains(46) {
		log.Fatal("Mutating the elements array returned from set.Elements() should not affect set membership")
	}

	if s.Cardinality() != 100 {
		log.Fatal("Mutating the elements array returned from set.Elements() should not affect set cardinality")
	}

	count := 100

	for range s.Elements() {
		count--
	}

	if count != 0 {
		log.Fatal("set.Iter() should return 100 items, but only got %d", 100-count)
	}

	counts := 1000
	countsChannel := make(chan int)

	for i := 0; i < 10; i++ {
		go func() {
			for range s.Elements() {
				countsChannel <- 1
			}
		}()
	}

Counting:
	for {
		select {
		case <-countsChannel:
			counts--
		case <-time.After(10 * time.Millisecond):
			break Counting
		}
	}

	if counts != 0 {
		t.Fatal("Should be able to read a set's contents from multiple threads at once.")
	}
}
package relation_test

import (
	"testing"

	"github.com/nlandolfi/set"
	"github.com/nlandolfi/set/relation"
)

var numbers set.Interface = set.New()

func init() {
	for n := 0; n < 100; n++ {
		numbers.Add(n)
	}
}

var equality relation.AbstractInterface = relation.NewFunctionBinaryRelation(numbers, func(x, y set.Element) bool {
	return x == y
})

var lessEqual relation.AbstractInterface = relation.NewFunctionBinaryRelation(numbers, func(x, y set.Element) bool {
	return x.(int) <= y.(int)
})

func BenchmarkSymmetric(b *testing.B) {
	for n := 0; n < b.N; n++ {
		if relation.Symmetric(equality) != true {
			b.Fatalf("The equality relation should be symmetric")
		}
	}