1
0
mirror of https://github.com/sjwhitworth/golearn.git synced 2025-04-28 13:48:56 +08:00
golearn/base/edf/thread_test.go

61 lines
1.7 KiB
Go
Raw Normal View History

2014-08-02 16:22:14 +01:00
package edf
import (
. "github.com/smartystreets/goconvey/convey"
"os"
2014-08-22 06:55:20 +00:00
"testing"
2014-08-02 16:22:14 +01:00
)
func TestThreadDeserialize(t *testing.T) {
2014-08-02 16:22:14 +01:00
bytes := []byte{0, 0, 0, 6, 83, 89, 83, 84, 69, 77, 0, 0, 0, 1}
Convey("Given a byte slice", t, func() {
var thread Thread
size := thread.Deserialize(bytes)
2014-08-02 16:22:14 +01:00
Convey("Decoded name should be SYSTEM", func() {
So(thread.name, ShouldEqual, "SYSTEM")
2014-08-02 16:22:14 +01:00
})
Convey("Size should be the same as the array", func() {
So(size, ShouldEqual, len(bytes))
})
})
}
func TestThreadSerialize(t *testing.T) {
var thread Thread
2014-08-02 16:22:14 +01:00
refBytes := []byte{0, 0, 0, 6, 83, 89, 83, 84, 69, 77, 0, 0, 0, 1}
thread.name = "SYSTEM"
thread.id = 1
2014-08-02 16:22:14 +01:00
toBytes := make([]byte, len(refBytes))
Convey("Should serialize correctly", t, func() {
thread.Serialize(toBytes)
2014-08-02 16:22:14 +01:00
So(toBytes, ShouldResemble, refBytes)
})
}
func TestThreadFindAndWrite(t *testing.T) {
Convey("Creating a non-existent file should succeed", t, func() {
2014-08-22 06:55:20 +00:00
tempFile, err := os.OpenFile("hello.db", os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0700) //ioutil.TempFile(os.TempDir(), "TestFileCreate")
2014-08-02 16:22:14 +01:00
So(err, ShouldEqual, nil)
2014-08-22 07:00:39 +00:00
Convey("Mapping the file should succeed", func() {
2014-08-02 16:22:14 +01:00
mapping, err := EdfMap(tempFile, EDF_CREATE)
So(err, ShouldEqual, nil)
2014-08-22 06:55:20 +00:00
Convey("Writing the thread should succeed", func() {
2014-08-02 16:22:14 +01:00
t := NewThread(mapping, "MyNameISWhat")
2014-08-22 06:55:20 +00:00
Convey("Thread number should be 3", func() {
2014-08-02 16:22:14 +01:00
So(t.id, ShouldEqual, 3)
})
Convey("Writing the thread should succeed", func() {
err := mapping.WriteThread(t)
So(err, ShouldEqual, nil)
2014-08-22 06:55:20 +00:00
Convey("Should be able to find the thread again later", func() {
2014-08-02 16:22:14 +01:00
id, err := mapping.FindThread("MyNameISWhat")
So(err, ShouldEqual, nil)
So(id, ShouldEqual, 3)
})
})
})
})
2014-08-19 06:24:22 +00:00
os.Remove("hello.db")
2014-08-02 16:22:14 +01:00
})
2014-08-19 06:24:22 +00:00
}