package rack

import "testing"

func TestRackBasics(t *testing.T) {
	r := New(26)
	if !r.Empty() || r.Total() != 0 {
		t.Fatal("new rack not empty")
	}

	r.Add(0) // a
	r.Add(0)
	r.Add(2) // c
	r.AddBlank()

	if r.Count(0) != 2 {
		t.Errorf("Count(a) = %d, want 2", r.Count(0))
	}
	if !r.Has(2) || r.Has(1) {
		t.Errorf("Has c=%v b=%v, want true,false", r.Has(2), r.Has(1))
	}
	if r.Blanks() != 1 {
		t.Errorf("Blanks = %d, want 1", r.Blanks())
	}
	if r.Total() != 4 {
		t.Errorf("Total = %d, want 4", r.Total())
	}

	r.Remove(0)
	if r.Count(0) != 1 {
		t.Errorf("after Remove, Count(a) = %d, want 1", r.Count(0))
	}
	r.RemoveBlank()
	if r.Blanks() != 0 {
		t.Errorf("after RemoveBlank, Blanks = %d, want 0", r.Blanks())
	}
}

func TestRackCloneIndependent(t *testing.T) {
	r := New(26)
	r.Add(0)
	cp := r.Clone()
	cp.Add(0)
	cp.AddBlank()
	if r.Count(0) != 1 || r.Blanks() != 0 {
		t.Errorf("mutating clone changed original: a=%d blanks=%d", r.Count(0), r.Blanks())
	}
	if cp.Count(0) != 2 || cp.Blanks() != 1 {
		t.Errorf("clone wrong: a=%d blanks=%d", cp.Count(0), cp.Blanks())
	}
}
