Skip to content

Commit dd85eed

Browse files
committed
Add tests for big struct fields
1 parent 73cc31c commit dd85eed

File tree

5 files changed

+129
-40
lines changed

5 files changed

+129
-40
lines changed

bindgen/ir/Struct.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ class Struct : public StructOrUnion,
7474
uint64_t typeSize;
7575

7676
/**
77-
* @return implicit helper class for struct that is represented as CStruct.
77+
* @return helper class methods for struct that is represented as CStruct.
7878
*/
7979
std::string generateHelperClassMethodsForStructRepresentation() const;
8080

8181
/**
82-
* @return implicit helper class for struct that is represented as CArray.
82+
* @return helper class methods for struct that is represented as CArray.
8383
*/
8484
std::string generateHelperClassMethodsForArrayRepresentation() const;
8585

tests/samples/Struct.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,34 @@ char getCharFromAnonymousStruct(struct structWithAnonymousStruct *s) {
3737
char getIntFromAnonymousStruct(struct structWithAnonymousStruct *s) {
3838
return s->anonymousStruct.i;
3939
}
40+
41+
int struct_test_long(struct bigStruct *s, enum struct_op op, long value) {
42+
switch (op) {
43+
case STRUCT_SET:
44+
s->one = value;
45+
return 1;
46+
case STRUCT_TEST:
47+
return s->one == value;
48+
}
49+
}
50+
51+
int struct_test_double(struct bigStruct *s, enum struct_op op, double value) {
52+
switch (op) {
53+
case STRUCT_SET:
54+
s->five = value;
55+
return 1;
56+
case STRUCT_TEST:
57+
return s->five == value;
58+
}
59+
}
60+
61+
int struct_test_point(struct bigStruct *s, enum struct_op op,
62+
struct point *value) {
63+
switch (op) {
64+
case STRUCT_SET:
65+
s->six = *value;
66+
return 1;
67+
case STRUCT_TEST:
68+
return (s->six.x == value->x) && (s->six.y == value->y);
69+
}
70+
}

tests/samples/Struct.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ struct bigStruct {
2626
int three;
2727
float four;
2828
double five;
29-
point_s six;
30-
int seven;
29+
struct point six;
30+
struct point *seven;
3131
int eight;
3232
int nine;
3333
int ten;
@@ -59,3 +59,10 @@ struct structWithAnonymousStruct {
5959
char getCharFromAnonymousStruct(struct structWithAnonymousStruct *s);
6060

6161
char getIntFromAnonymousStruct(struct structWithAnonymousStruct *s);
62+
63+
enum struct_op { STRUCT_SET, STRUCT_TEST };
64+
65+
int struct_test_long(struct bigStruct *s, enum struct_op op, long value);
66+
int struct_test_double(struct bigStruct *s, enum struct_op op, double value);
67+
int struct_test_point(struct bigStruct *s, enum struct_op op,
68+
struct point *value);

tests/samples/Struct.scala

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ object Struct {
1313
type point_s = native.Ptr[struct_point]
1414
type struct_bigStruct = native.CArray[Byte, native.Nat.Digit[native.Nat._1, native.Nat.Digit[native.Nat._1, native.Nat._2]]]
1515
type struct_structWithAnonymousStruct = native.CStruct2[native.CInt, native.CArray[Byte, native.Nat._8]]
16+
type enum_struct_op = native.CUnsignedInt
1617
def setPoints(points: native.Ptr[struct_points], x1: native.CInt, y1: native.CInt, x2: native.CInt, y2: native.CInt): Unit = native.extern
1718
def getPoint(points: native.Ptr[struct_points], pointIndex: enum_pointIndex): native.CInt = native.extern
1819
def createPoint(): native.Ptr[struct_point] = native.extern
1920
def getBigStructSize(): native.CInt = native.extern
2021
def getCharFromAnonymousStruct(s: native.Ptr[struct_structWithAnonymousStruct]): native.CChar = native.extern
2122
def getIntFromAnonymousStruct(s: native.Ptr[struct_structWithAnonymousStruct]): native.CChar = native.extern
23+
def struct_test_long(s: native.Ptr[struct_bigStruct], op: enum_struct_op, value: native.CLong): native.CInt = native.extern
24+
def struct_test_double(s: native.Ptr[struct_bigStruct], op: enum_struct_op, value: native.CDouble): native.CInt = native.extern
25+
def struct_test_point(s: native.Ptr[struct_bigStruct], op: enum_struct_op, value: native.Ptr[struct_point]): native.CInt = native.extern
2226
}
2327

2428
import Struct._
@@ -28,6 +32,9 @@ object StructEnums {
2832
final val enum_pointIndex_Y1: enum_pointIndex = 1.toUInt
2933
final val enum_pointIndex_X2: enum_pointIndex = 2.toUInt
3034
final val enum_pointIndex_Y2: enum_pointIndex = 3.toUInt
35+
36+
final val enum_struct_op_STRUCT_SET: enum_struct_op = 0.toUInt
37+
final val enum_struct_op_STRUCT_TEST: enum_struct_op = 1.toUInt
3138
}
3239

3340
object StructHelpers {
@@ -61,42 +68,42 @@ object StructHelpers {
6168
def four_=(value: native.CFloat): Unit = !(p._1 + 16).cast[native.Ptr[native.CFloat]] = value
6269
def five: native.CDouble = !(p._1 + 24).cast[native.Ptr[native.CDouble]]
6370
def five_=(value: native.CDouble): Unit = !(p._1 + 24).cast[native.Ptr[native.CDouble]] = value
64-
def six: native.Ptr[struct_point] = !(p._1 + 32).cast[native.Ptr[native.Ptr[struct_point]]]
65-
def six_=(value: native.Ptr[struct_point]): Unit = !(p._1 + 32).cast[native.Ptr[native.Ptr[struct_point]]] = value
66-
def seven: native.CInt = !(p._1 + 40).cast[native.Ptr[native.CInt]]
67-
def seven_=(value: native.CInt): Unit = !(p._1 + 40).cast[native.Ptr[native.CInt]] = value
68-
def eight: native.CInt = !(p._1 + 44).cast[native.Ptr[native.CInt]]
69-
def eight_=(value: native.CInt): Unit = !(p._1 + 44).cast[native.Ptr[native.CInt]] = value
70-
def nine: native.CInt = !(p._1 + 48).cast[native.Ptr[native.CInt]]
71-
def nine_=(value: native.CInt): Unit = !(p._1 + 48).cast[native.Ptr[native.CInt]] = value
72-
def ten: native.CInt = !(p._1 + 52).cast[native.Ptr[native.CInt]]
73-
def ten_=(value: native.CInt): Unit = !(p._1 + 52).cast[native.Ptr[native.CInt]] = value
74-
def eleven: native.CInt = !(p._1 + 56).cast[native.Ptr[native.CInt]]
75-
def eleven_=(value: native.CInt): Unit = !(p._1 + 56).cast[native.Ptr[native.CInt]] = value
76-
def twelve: native.CInt = !(p._1 + 60).cast[native.Ptr[native.CInt]]
77-
def twelve_=(value: native.CInt): Unit = !(p._1 + 60).cast[native.Ptr[native.CInt]] = value
78-
def thirteen: native.CInt = !(p._1 + 64).cast[native.Ptr[native.CInt]]
79-
def thirteen_=(value: native.CInt): Unit = !(p._1 + 64).cast[native.Ptr[native.CInt]] = value
80-
def fourteen: native.CInt = !(p._1 + 68).cast[native.Ptr[native.CInt]]
81-
def fourteen_=(value: native.CInt): Unit = !(p._1 + 68).cast[native.Ptr[native.CInt]] = value
82-
def fifteen: native.CInt = !(p._1 + 72).cast[native.Ptr[native.CInt]]
83-
def fifteen_=(value: native.CInt): Unit = !(p._1 + 72).cast[native.Ptr[native.CInt]] = value
84-
def sixteen: native.CInt = !(p._1 + 76).cast[native.Ptr[native.CInt]]
85-
def sixteen_=(value: native.CInt): Unit = !(p._1 + 76).cast[native.Ptr[native.CInt]] = value
86-
def seventeen: native.CInt = !(p._1 + 80).cast[native.Ptr[native.CInt]]
87-
def seventeen_=(value: native.CInt): Unit = !(p._1 + 80).cast[native.Ptr[native.CInt]] = value
88-
def eighteen: native.CInt = !(p._1 + 84).cast[native.Ptr[native.CInt]]
89-
def eighteen_=(value: native.CInt): Unit = !(p._1 + 84).cast[native.Ptr[native.CInt]] = value
90-
def nineteen: native.CInt = !(p._1 + 88).cast[native.Ptr[native.CInt]]
91-
def nineteen_=(value: native.CInt): Unit = !(p._1 + 88).cast[native.Ptr[native.CInt]] = value
92-
def twenty: native.CInt = !(p._1 + 92).cast[native.Ptr[native.CInt]]
93-
def twenty_=(value: native.CInt): Unit = !(p._1 + 92).cast[native.Ptr[native.CInt]] = value
94-
def twentyOne: native.CInt = !(p._1 + 96).cast[native.Ptr[native.CInt]]
95-
def twentyOne_=(value: native.CInt): Unit = !(p._1 + 96).cast[native.Ptr[native.CInt]] = value
96-
def twentyTwo: native.CInt = !(p._1 + 100).cast[native.Ptr[native.CInt]]
97-
def twentyTwo_=(value: native.CInt): Unit = !(p._1 + 100).cast[native.Ptr[native.CInt]] = value
98-
def twentyThree: native.CInt = !(p._1 + 104).cast[native.Ptr[native.CInt]]
99-
def twentyThree_=(value: native.CInt): Unit = !(p._1 + 104).cast[native.Ptr[native.CInt]] = value
71+
def six: native.Ptr[struct_point] = (p._1 + 32).cast[native.Ptr[struct_point]]
72+
def six_=(value: native.Ptr[struct_point]): Unit = !(p._1 + 32).cast[native.Ptr[struct_point]] = !value
73+
def seven: native.Ptr[struct_point] = !(p._1 + 40).cast[native.Ptr[native.Ptr[struct_point]]]
74+
def seven_=(value: native.Ptr[struct_point]): Unit = !(p._1 + 40).cast[native.Ptr[native.Ptr[struct_point]]] = value
75+
def eight: native.CInt = !(p._1 + 48).cast[native.Ptr[native.CInt]]
76+
def eight_=(value: native.CInt): Unit = !(p._1 + 48).cast[native.Ptr[native.CInt]] = value
77+
def nine: native.CInt = !(p._1 + 52).cast[native.Ptr[native.CInt]]
78+
def nine_=(value: native.CInt): Unit = !(p._1 + 52).cast[native.Ptr[native.CInt]] = value
79+
def ten: native.CInt = !(p._1 + 56).cast[native.Ptr[native.CInt]]
80+
def ten_=(value: native.CInt): Unit = !(p._1 + 56).cast[native.Ptr[native.CInt]] = value
81+
def eleven: native.CInt = !(p._1 + 60).cast[native.Ptr[native.CInt]]
82+
def eleven_=(value: native.CInt): Unit = !(p._1 + 60).cast[native.Ptr[native.CInt]] = value
83+
def twelve: native.CInt = !(p._1 + 64).cast[native.Ptr[native.CInt]]
84+
def twelve_=(value: native.CInt): Unit = !(p._1 + 64).cast[native.Ptr[native.CInt]] = value
85+
def thirteen: native.CInt = !(p._1 + 68).cast[native.Ptr[native.CInt]]
86+
def thirteen_=(value: native.CInt): Unit = !(p._1 + 68).cast[native.Ptr[native.CInt]] = value
87+
def fourteen: native.CInt = !(p._1 + 72).cast[native.Ptr[native.CInt]]
88+
def fourteen_=(value: native.CInt): Unit = !(p._1 + 72).cast[native.Ptr[native.CInt]] = value
89+
def fifteen: native.CInt = !(p._1 + 76).cast[native.Ptr[native.CInt]]
90+
def fifteen_=(value: native.CInt): Unit = !(p._1 + 76).cast[native.Ptr[native.CInt]] = value
91+
def sixteen: native.CInt = !(p._1 + 80).cast[native.Ptr[native.CInt]]
92+
def sixteen_=(value: native.CInt): Unit = !(p._1 + 80).cast[native.Ptr[native.CInt]] = value
93+
def seventeen: native.CInt = !(p._1 + 84).cast[native.Ptr[native.CInt]]
94+
def seventeen_=(value: native.CInt): Unit = !(p._1 + 84).cast[native.Ptr[native.CInt]] = value
95+
def eighteen: native.CInt = !(p._1 + 88).cast[native.Ptr[native.CInt]]
96+
def eighteen_=(value: native.CInt): Unit = !(p._1 + 88).cast[native.Ptr[native.CInt]] = value
97+
def nineteen: native.CInt = !(p._1 + 92).cast[native.Ptr[native.CInt]]
98+
def nineteen_=(value: native.CInt): Unit = !(p._1 + 92).cast[native.Ptr[native.CInt]] = value
99+
def twenty: native.CInt = !(p._1 + 96).cast[native.Ptr[native.CInt]]
100+
def twenty_=(value: native.CInt): Unit = !(p._1 + 96).cast[native.Ptr[native.CInt]] = value
101+
def twentyOne: native.CInt = !(p._1 + 100).cast[native.Ptr[native.CInt]]
102+
def twentyOne_=(value: native.CInt): Unit = !(p._1 + 100).cast[native.Ptr[native.CInt]] = value
103+
def twentyTwo: native.CInt = !(p._1 + 104).cast[native.Ptr[native.CInt]]
104+
def twentyTwo_=(value: native.CInt): Unit = !(p._1 + 104).cast[native.Ptr[native.CInt]] = value
105+
def twentyThree: native.CInt = !(p._1 + 108).cast[native.Ptr[native.CInt]]
106+
def twentyThree_=(value: native.CInt): Unit = !(p._1 + 108).cast[native.Ptr[native.CInt]] = value
100107
}
101108

102109
def struct_bigStruct()(implicit z: native.Zone): native.Ptr[struct_bigStruct] = native.alloc[struct_bigStruct]

tests/samples/src/test/scala/org/scalanative/bindgen/samples/StructTests.scala

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,49 @@ object StructTests extends TestSuite {
6060
assert(42 == Struct.getIntFromAnonymousStruct(structWithAnonymousStruct))
6161
}
6262
}
63+
64+
'getFieldOfBigStruct - {
65+
Zone { implicit zone: Zone =>
66+
val structPtr = alloc[Struct.struct_bigStruct]
67+
for (value <- Seq(Long.MinValue, -1, 0, 1, Long.MaxValue)) {
68+
Struct.struct_test_long(structPtr, enum_struct_op_STRUCT_SET, value)
69+
assert(structPtr.one == value)
70+
}
71+
72+
for (value <- Seq(Double.MinValue, -1, 0, 1, Double.MaxValue)) {
73+
Struct.struct_test_double(structPtr, enum_struct_op_STRUCT_SET, value)
74+
assert(structPtr.five == value)
75+
}
76+
77+
val pointPtr = alloc[Struct.point]
78+
pointPtr.x = 5
79+
pointPtr.y = 10
80+
81+
Struct.struct_test_point(structPtr, enum_struct_op_STRUCT_SET, pointPtr)
82+
assert(structPtr.six.x == pointPtr.x)
83+
assert(structPtr.six.y == pointPtr.y)
84+
}
85+
}
86+
87+
'setFieldOfBigStruct - {
88+
Zone { implicit zone: Zone =>
89+
val structPtr = alloc[Struct.struct_bigStruct]
90+
for (value <- Seq(Long.MinValue, -1, 0, 1, Long.MaxValue)) {
91+
structPtr.one = value
92+
assert(Struct.struct_test_long(structPtr, enum_struct_op_STRUCT_TEST, value) == 1)
93+
}
94+
95+
for (value <- Seq(Double.MinValue, -1, 0, 1, Double.MaxValue)) {
96+
structPtr.five = value
97+
assert(Struct.struct_test_double(structPtr, enum_struct_op_STRUCT_TEST, value) == 1)
98+
}
99+
100+
val pointPtr = alloc[Struct.point]
101+
pointPtr.x = 5
102+
pointPtr.y = 10
103+
structPtr.six = pointPtr
104+
assert(Struct.struct_test_point(structPtr, enum_struct_op_STRUCT_TEST, pointPtr) == 1)
105+
}
106+
}
63107
}
64108
}

0 commit comments

Comments
 (0)