123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- /*
- * Public Domain Software
- *
- * I (Matthias Ladkau) am the author of the source code in this file.
- * I have placed the source code in this file in the public domain.
- *
- * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
- */
- package datautil
- import (
- "flag"
- "fmt"
- "os"
- "testing"
- "devt.de/krotik/common/fileutil"
- )
- const testdbdir = "test"
- const invalidFileName = "**\x00"
- func TestMain(m *testing.M) {
- flag.Parse()
- // Setup
- if res, _ := fileutil.PathExists(testdbdir); res {
- os.RemoveAll(testdbdir)
- }
- err := os.Mkdir(testdbdir, 0770)
- if err != nil {
- fmt.Print("Could not create test directory:", err.Error())
- os.Exit(1)
- }
- // Run the tests
- res := m.Run()
- // Teardown
- err = os.RemoveAll(testdbdir)
- if err != nil {
- fmt.Print("Could not remove test directory:", err.Error())
- }
- os.Exit(res)
- }
- func TestPersistentMap(t *testing.T) {
- // Test main scenario
- pm, err := NewPersistentMap(testdbdir + "/testmap.map")
- if err != nil {
- t.Error(nil)
- return
- }
- pm.Data["test1"] = "test1data"
- pm.Data["test2"] = "test2data"
- pm.Flush()
- pm2, _ := LoadPersistentMap(testdbdir + "/testmap.map")
- if len(pm2.Data) != 2 {
- t.Error("Unexpected size of map")
- return
- }
- if pm.Data["test1"] != "test1data" || pm.Data["test2"] != "test2data" {
- t.Error("Unexpected data in map:", pm.Data)
- return
- }
- // Test error cases
- pm, err = NewPersistentMap(invalidFileName)
- if err == nil {
- t.Error("Unexpected result of new map")
- return
- }
- pm, err = LoadPersistentMap(invalidFileName)
- if err == nil {
- t.Error("Unexpected result of new map")
- return
- }
- pm = &PersistentMap{invalidFileName, make(map[string]interface{})}
- if err := pm.Flush(); err == nil {
- t.Error("Unexpected result of new map")
- return
- }
- }
- func TestPersistentStringMap(t *testing.T) {
- // Test main scenario
- pm, err := NewPersistentStringMap(testdbdir + "/teststringmap.map")
- if err != nil {
- t.Error(nil)
- return
- }
- pm.Data["test1"] = "test1data"
- pm.Data["test2"] = "test2data"
- pm.Flush()
- pm2, _ := LoadPersistentStringMap(testdbdir + "/teststringmap.map")
- if len(pm2.Data) != 2 {
- t.Error("Unexpected size of map")
- return
- }
- if pm.Data["test1"] != "test1data" || pm.Data["test2"] != "test2data" {
- t.Error("Unexpected data in map:", pm.Data)
- return
- }
- // Test error cases
- pm, err = NewPersistentStringMap(invalidFileName)
- if err == nil {
- t.Error("Unexpected result of new map")
- return
- }
- pm, err = LoadPersistentStringMap(invalidFileName)
- if err == nil {
- t.Error("Unexpected result of new map")
- return
- }
- pm = &PersistentStringMap{invalidFileName, make(map[string]string)}
- if err := pm.Flush(); err == nil {
- t.Error("Unexpected result of new map")
- return
- }
- }
|