-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd68ebb
commit 88d2226
Showing
2 changed files
with
139 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
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") | ||
} | ||
} |
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,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) | ||
} | ||
} |