1. ww
  2. cabinet

Commits

w...@styx.org  committed afc6907

figure out how incrint works

  • Participants
  • Parent commits ebf0a7d
  • Branches default

Comments (0)

Files changed (1)

File cabinet_test.go

View file
  • Ignore whitespace
 	"fmt"
 	"testing"
 	"os"
-	"unsafe"
+	"bytes"
+	"encoding/binary"
 )
 
 var test_db string = "casket.kch"
 	}
 }
 
+
 func TestIncr(t *testing.T) {
-	return
 	kc := newCabinet(t, test_db)
 	defer delCabinet(t, kc)
 
-	bt := make([]byte, 0)
-	n := uint64(1)
-	np := unsafe.Unreflect(unsafe.Typeof(bt), unsafe.Pointer(&n)).([]byte)
-	kc.Set([]byte("numbers"), np)
 	err := kc.IncrInt([]byte("numbers"), 6)
 	if err != nil {
-		t.Error(err)
+		t.Error("IncrInt(6): %s", err)
 	}
+
 	v, err := kc.Get([]byte("numbers"))
-	vp := unsafe.Unreflect(unsafe.Typeof(n), unsafe.Pointer(&v[0])).(uint64)
-	if vp != 7 {
-		t.Errorf("zada: %s", vp)
+	var n uint64
+	binary.Read(bytes.NewBuffer(v), binary.BigEndian, &n)
+	if n != 6 {
+		t.Errorf("IncrInt(6): expected 6 got %d", n)
 	}
+
+	err = kc.IncrInt([]byte("numbers"), 6)
+	if err != nil {
+		t.Error("IncrInt(6): %s", err)
+	}
+
+	v, err = kc.Get([]byte("numbers"))
+	binary.Read(bytes.NewBuffer(v), binary.BigEndian, &n)
+	if n != 12 {
+		t.Errorf("IncrInt(6): expected 12 got %d", n)
+	}
+
+	return
+
+	err = kc.IncrDouble([]byte("floats"), 1.5)
+	if err != nil {
+		t.Errorf("IncrDouble(1.5): %s", err)
+	}
+
+	var d float64
+	v, err = kc.Get([]byte("floats"))
+	binary.Read(bytes.NewBuffer(v), binary.BigEndian, &d)
+	if d != 1.5 {
+		t.Errorf("IncrDouble(1.5): expected 1.5 got %l", d)
+	}
+
+	err = kc.IncrDouble([]byte("floats"), 2.0)
+	if err != nil {
+		t.Errorf("IncrDouble(2.0): %s", err)
+	}
+
+	v, err = kc.Get([]byte("floats"))
+	binary.Read(bytes.NewBuffer(v), binary.BigEndian, &d)
+	if d != 12 {
+		t.Errorf("IncrDouble(2.0): expected 3.5 got %l", d)
+	}
+
 }
 
 func TestIterators(t *testing.T) {