-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #4 - Add immutable remove operation
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package trie | ||
|
||
import ( | ||
"testing" | ||
"math/rand" | ||
) | ||
|
||
func TestImmutableRemoveIsImmutable(t *testing.T) { | ||
for _, keySet := range testAddSamples { | ||
trie := FromKeys(keySet.Keys) | ||
for _, key := range keySet.Keys { | ||
updated := Remove(trie, key) | ||
if Equal(trie, updated) { | ||
t.Fatalf("immuatble remove should not mutate trie, original: %v, updated: %v", trie, updated) | ||
} | ||
trie = updated | ||
} | ||
} | ||
} | ||
|
||
func TestMutableAndImmutableRemoveSame(t *testing.T) { | ||
for _, keySet := range append(testAddSamples, randomTestAddSamples(100)...) { | ||
mut := FromKeys(keySet.Keys) | ||
immut := FromKeys(keySet.Keys) | ||
|
||
for _, key := range keySet.Keys { | ||
mut.Remove(key) | ||
immut = Remove(immut, key) | ||
if d := mut.CheckInvariant(); d != nil { | ||
t.Fatalf("mutable trie invariant discrepancy: %v", d) | ||
} | ||
if d := immut.CheckInvariant(); d != nil { | ||
t.Fatalf("immutable trie invariant discrepancy: %v", d) | ||
} | ||
if !Equal(mut, immut) { | ||
t.Errorf("mutable trie %v differs from immutable trie %v", mut, immut) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestRemoveIsOrderIndependent(t *testing.T) { | ||
for _, keySet := range append(testAddSamples, randomTestAddSamples(100)...) { | ||
mut := FromKeys(keySet.Keys) | ||
immut := FromKeys(keySet.Keys) | ||
|
||
for j := 0; j < 100; j++ { | ||
perm := rand.Perm(len(keySet.Keys)) | ||
for _, idx := range perm { | ||
mut.Remove(keySet.Keys[idx]) | ||
immut = Remove(immut, keySet.Keys[idx]) | ||
|
||
if d := immut.CheckInvariant(); d != nil { | ||
t.Fatalf("trie invariant discrepancy: %v", d) | ||
} | ||
if !Equal(mut, immut) { | ||
t.Errorf("trie %v differs from trie %v", mut, immut) | ||
} | ||
} | ||
} | ||
} | ||
} |