Skip to content

Commit

Permalink
Adding a unit case to verify rollover
Browse files Browse the repository at this point in the history
Signed-off-by: Abhinandan Prativadi <abhi@docker.com>
  • Loading branch information
abhi committed Jun 9, 2017
1 parent 181dccc commit 61b4076
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions bitseq/sequence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ func TestSetUnset(t *testing.T) {
t.Fatal(err)
}
}

if _, err := hnd.SetAny(); err != ErrNoBitAvailable {
t.Fatal("Expected error. Got success")
}
Expand Down Expand Up @@ -908,6 +909,7 @@ func TestAllocateRandomDeallocate(t *testing.T) {
t.Fatalf("Unexpected failure on allocation %d: %v\n%s", i, err, hnd)
}
}

if hnd.Unselected() != uint64(numBits/2) {
t.Fatalf("Expected full sequence. Instead found %d free bits. %s", hnd.unselected, hnd)
}
Expand Down Expand Up @@ -1133,3 +1135,76 @@ func TestIsCorrupted(t *testing.T) {
}
}
}

func TestSetRollover(t *testing.T) {
ds, err := randomLocalStore()
if err != nil {
t.Fatal(err)
}

numBlocks := uint32(8)
numBits := int(numBlocks * blockLen)
hnd, err := NewHandle("bitseq-test/data/", ds, "test1", uint64(numBits))
if err != nil {
t.Fatal(err)
}

// Allocate first half of the bits
for i := 0; i < numBits/2; i++ {
_, err := hnd.SetAny()
if err != nil {
t.Fatalf("Unexpected failure on allocation %d: %v\n%s", i, err, hnd)
}
}

if hnd.Unselected() != uint64(numBits/2) {
t.Fatalf("Expected full sequence. Instead found %d free bits. %s", hnd.unselected, hnd)
}

seed := time.Now().Unix()
rand.Seed(seed)

// Deallocate half of the allocated bits following a random pattern
pattern := rand.Perm(numBits / 2)
for i := 0; i < numBits/4; i++ {
bit := pattern[i]
err := hnd.Unset(uint64(bit))
if err != nil {
t.Fatalf("Unexpected failure on deallocation of %d: %v.\nSeed: %d.\n%s", bit, err, seed, hnd)
}
}
if hnd.Unselected() != uint64(3*numBits/4) {
t.Fatalf("Expected full sequence. Instead found %d free bits.\nSeed: %d.\n%s", hnd.unselected, seed, hnd)
}

//request to allocate for remaining half of the bits
for i := 0; i < numBits/2; i++ {
_, err := hnd.SetAny()
if err != nil {
t.Fatalf("Unexpected failure on allocation %d: %v\nSeed: %d\n%s", i, err, seed, hnd)
}
}

//At this point all the bits must be allocated except the randomly unallocated bits
//which were unallocated in the first half of the bit sequence
if hnd.Unselected() != uint64(numBits/4) {
t.Fatalf("Unexpected number of unselected bits %d, Expected %d", hnd.Unselected(), numBits/4)
}

for i := 0; i < numBits/4; i++ {
_, err := hnd.SetAny()
if err != nil {
t.Fatalf("Unexpected failure on allocation %d: %v\nSeed: %d\n%s", i, err, seed, hnd)
}
}
//Now requesting to allocate the unallocated random bits (qurter of the number of bits) should
//leave no more bits that can be allocated.
if hnd.Unselected() != 0 {
t.Fatalf("Unexpected number of unselected bits %d, Expected %d", hnd.Unselected(), numBits/4)
}

err = hnd.Destroy()
if err != nil {
t.Fatal(err)
}
}

0 comments on commit 61b4076

Please sign in to comment.