Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hunter-bera committed Jul 4, 2024
1 parent bd68ebb commit 88d2226
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
63 changes: 63 additions & 0 deletions tools/rwstore/rwlist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package rwstore

import (
"testing"
)

func TestNewRWList(t *testing.T) {
list := NewRWList[int]()
if list == nil {
t.Error("NewRWList did not create a new list.")
}
}

func TestGet(t *testing.T) {
list := NewRWList[int]()
list.Append(10)
value, ok := list.Get(0)
if !ok || value != 10 {
t.Errorf("Get failed, expected 10 got %v", value)
}
_, ok = list.Get(-1)
if ok {
t.Error("Get should return false for negative index")
}
_, ok = list.Get(1)
if ok {
t.Error("Get should return false for out-of-range index")
}
}

func TestReplace(t *testing.T) {
list := NewRWList[int]()
list.Append(10)
list.Replace([]int{20, 30})
if len(list.Copy()) != 2 {
t.Error("Replace did not work as expected")
}
}

func TestAppend(t *testing.T) {
list := NewRWList[int]()
list.Append(10)
if len(list.Copy()) != 1 {
t.Error("Append did not increase the length of the list")
}
if list.a[0] != 10 {
t.Errorf("Append did not append the correct value, got %v", list.a[0])
}
}

func TestCopy(t *testing.T) {
list := NewRWList[int]()
list.Append(10)
copiedList := list.Copy()
if len(copiedList) != 1 || copiedList[0] != 10 {
t.Error("Copy did not copy the list correctly")
}
// Modify original list and ensure copy is unaffected
list.Append(20)
if len(copiedList) != 1 {
t.Error("Copy was affected by changes to the original list")
}
}
76 changes: 76 additions & 0 deletions tools/rwstore/rwmap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package rwstore

import (
"sync"
"testing"
)

func TestRWMap(t *testing.T) {
rwMap := NewRWMap[int, string]()

// Test Set and Get
rwMap.Set(1, "one")
if v, ok := rwMap.Get(1); !ok || v != "one" {
t.Errorf("Set or Get is not working, expected 'one', got %v", v)
}

// Test Exists
if !rwMap.Exists(1) {
t.Errorf("Exists is not working, expected true, got false")
}

// Test Len
if rwMap.Len() != 1 {
t.Errorf("Len is not working, expected 1, got %d", rwMap.Len())
}

// Test Delete
rwMap.Delete(1)
if rwMap.Exists(1) {
t.Errorf("Delete is not working, key 1 still exists")
}

// Test Iterate
rwMap.Set(2, "two")
rwMap.Set(3, "three")
visited := make(map[int]bool)
rwMap.Iterate(func(k int, _ string) bool {
visited[k] = true
return true
})
if len(visited) != 2 || !visited[2] || !visited[3] {
t.Errorf("Iterate is not working correctly, visited map: %+v", visited)
}
}

func TestRWMapConcurrentAccess(t *testing.T) {
rwMap := NewRWMap[int, int]()
var wg sync.WaitGroup

// Perform concurrent writes
for i := 0; i < 100; i++ {
wg.Add(1)
go func(val int) {
defer wg.Done()
rwMap.Set(val, val)
}(i)
}

// Perform concurrent reads
for i := 0; i < 100; i++ {
wg.Add(1)
go func(val int) {
defer wg.Done()
if v, ok := rwMap.Get(val); !ok || v != val {
t.Errorf("Concurrent access failed, expected %d, got %d", val, v)
}
}(i)
}

wg.Wait()

// Check final map length
if l := rwMap.Len(); l != 100 {
t.Errorf("Concurrent writes failed, expected map length 100, got %d", l)
}
}

0 comments on commit 88d2226

Please sign in to comment.