forked from tchajed/goose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs.go
148 lines (115 loc) · 2.09 KB
/
structs.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package semantics
type TwoInts struct {
x uint64
y uint64
}
type S struct {
a uint64
b TwoInts
c bool
}
func NewS() *S {
return &S{
a: 2,
b: TwoInts{x: 1, y: 2},
c: true,
}
}
func (s *S) readA() uint64 {
return s.a
}
func (s *S) readB() TwoInts {
return s.b
}
func (s S) readBVal() TwoInts {
return s.b
}
func (s *S) updateBValX(i uint64) {
s.b.x = i
}
func (s *S) negateC() {
s.c = !s.c
}
func failing_testStructUpdates() bool {
var ok = true
ns := NewS()
ok = ok && (ns.readA() == 2)
var b1 = ns.readB()
ok = ok && (b1.x == 1)
ns.negateC()
ok = ok && (ns.c == false)
b1.x = 3
var b2 = ns.readB()
ok = ok && (b2.x == 1)
var b3 = &ns.b
ok = ok && b3.x == 1
ns.updateBValX(4)
ok = ok && (ns.readBVal().x == 4)
return ok
}
func testNestedStructUpdates() bool {
var ok = true
var ns = NewS()
ns.b.x = 5
ok = ok && ns.b.x == 5
ns = NewS()
var p = &ns.b
p.x = 5
ok = ok && ns.b.x == 5
ns = NewS()
p = &ns.b
ns.b.x = 5
ok = ok && (*p).x == 5
ns = NewS()
p = &ns.b
ns.b.x = 5
ok = ok && p.x == 5
return ok
}
func testStructConstructions() bool {
var ok = true
var p1 *TwoInts // p1 == nil
var p2 TwoInts // p2 == TwoInts{0, 0}
p3 := TwoInts{y: 0, x: 0} // p3 == TwoInts{0, 0}
p4 := TwoInts{x: 0, y: 0} // p4 == TwoInts{0, 0}
ok = ok && (p1 == nil)
p1 = new(TwoInts) // p1 == &TwoInts{0, 0}
ok = ok && (p2 == p3)
ok = ok && (p3 == p4)
ok = ok && (p4 == *p1)
ok = ok && (&p4 != p1)
return ok
}
func testIncompleteStruct() bool {
var ok = true
p1 := TwoInts{x: 0}
ok = ok && (p1.y == 0)
p2 := S{a: 2}
ok = ok && (p2.b.x == 0)
ok = ok && (p2.c == false)
return ok
}
type StructWrap struct {
i uint64
}
func testStoreInStructVar() bool {
var p StructWrap = StructWrap{i: 0}
p.i = 5
return p.i == 5
}
func testStoreInStructPointerVar() bool {
var p *StructWrap = new(StructWrap)
p.i = 5
return p.i == 5
}
func testStoreComposite() bool {
p := new(TwoInts)
*p = TwoInts{x: 3, y: 4}
return (*p).y == 4
}
func testStoreSlice() bool {
p := new([]uint64)
s := make([]uint64, 3)
*p = s
return uint64(len(*p)) == uint64(3)
}