-
Notifications
You must be signed in to change notification settings - Fork 0
/
morfos_test.go
79 lines (62 loc) · 1.31 KB
/
morfos_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package morfos_test
import (
"testing"
"unsafe"
"github.com/katcipis/morfos"
)
func TestStructsSameSize(t *testing.T) {
type original struct {
x int
y int
}
type notoriginal struct {
z int
w int
}
orig := original{x: 100, y: 200}
_, ok := interface{}(orig).(notoriginal)
if ok {
t.Fatal("casting should be invalid")
}
morphed := morfos.Morph(orig, notoriginal{})
morphedNotOriginal, ok := morphed.(notoriginal)
if !ok {
t.Fatal("casting should be valid now")
}
if orig.x != morphedNotOriginal.z {
t.Fatalf("expected x[%d] == z[%d]", orig.x, morphedNotOriginal.z)
}
if orig.y != morphedNotOriginal.w {
t.Fatalf("expected y[%d] == w[%d]", orig.y, morphedNotOriginal.w)
}
}
func TestMutatingString(t *testing.T) {
type stringStruct struct {
str unsafe.Pointer
len int
}
var rawstr [5]byte
rawstr[0] = 'h'
rawstr[1] = 'e'
rawstr[2] = 'l'
rawstr[3] = 'l'
rawstr[4] = 'o'
hi := stringStruct{
str: unsafe.Pointer(&rawstr),
len: len(rawstr),
}
somestr := ""
morphed := morfos.Morph(hi, somestr)
mutableStr := morphed.(string)
if mutableStr != "hello" {
t.Fatalf("expected hello, got: %s", mutableStr)
}
rawstr[0] = 'h'
rawstr[1] = 'a'
rawstr[2] = 'c'
rawstr[3] = 'k'
rawstr[4] = 'd'
if mutableStr != "hackd" {
t.Fatalf("expected hackd, got: %s", mutableStr)
}
}