Skip to content

Commit

Permalink
Added tests for the readstring and writstring functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Ritchie6 committed Dec 18, 2024
1 parent 0b8c7e3 commit f69fcd4
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions test/memoria_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test
import (
"bytes"
"os"
"strings"
"testing"

memoria "github.com/IMGIITRoorkee/Memoria_Simple"
Expand Down Expand Up @@ -79,3 +80,77 @@ func TestMemoriaWriteRead(t *testing.T) {
})
}
}

func TestMemoriaWriteReadString(t1 *testing.T) {

// Create temporary directory for tests
tempDir1, err1 := os.MkdirTemp("", "memoria-test-*")
if err1 != nil {
t1.Fatalf("Failed to create temp dir: %v", err1)
}
defer os.RemoveAll(tempDir1)

m := memoria.New(memoria.Options{
Basedir: tempDir1,
MaxCacheSize: 1024,
})

tests := []struct {
name string
key string
value string
wantErr bool
}{
{
name: "Simple write and read (String Wrapper)",
key: "test1",
value: string("hello world"),
wantErr: false,
},
{
name: "Empty key",
key: "",
value: string("test"),
wantErr: true,
},
{
name: "Empty value",
key: "test2",
value: string(""),
wantErr: false,
},
{
name: "Large value",
key: "test3",
value: strings.Repeat("a", 1000),
wantErr: false,
},
}

for _, tt1 := range tests {
t1.Run(tt1.name, func(t1 *testing.T) {

// Test WriteString
err1 := m.WriteString(tt1.key, tt1.value)
if (err1 != nil) != tt1.wantErr {
t1.Errorf("Write() error = %v, wantErr %v", err1, tt1.wantErr)
return
}

if tt1.wantErr {
return
}

// Test ReadString
got1, err1 := m.ReadString(tt1.key)
if err1 != nil {
t1.Errorf("Read() error = %v", err1)
return
}

if got1 != tt1.value {
t1.Errorf("Read() got = %v, want %v", got1, tt1.value)
}
})
}
}

0 comments on commit f69fcd4

Please sign in to comment.