Skip to content

Commit 65450fd

Browse files
committed
Add tests for big struct fields
1 parent b26ec93 commit 65450fd

File tree

6 files changed

+130
-41
lines changed

6 files changed

+130
-41
lines changed

bindgen/ir/Struct.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ std::string Struct::generateHelperClass() const {
9595

9696
bool Struct::hasHelperMethods() const {
9797
if (!isRepresentedAsStruct()) {
98-
return false;
98+
return !fields.empty();
9999
}
100100
return !isPacked && !fields.empty();
101101
}

bindgen/ir/Struct.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ class Struct : public StructOrUnion,
8383
bool isRepresentedAsStruct() const;
8484

8585
/**
86-
* @return implicit helper class for struct that is represented as CStruct.
86+
* @return helper class methods for struct that is represented as CStruct.
8787
*/
8888
std::string generateHelperClassMethodsForStructRepresentation() const;
8989

9090
/**
91-
* @return implicit helper class for struct that is represented as CArray.
91+
* @return helper class methods for struct that is represented as CArray.
9292
*/
9393
std::string generateHelperClassMethodsForArrayRepresentation() const;
9494

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;
@@ -63,3 +63,10 @@ struct __attribute__((__packed__)) packedStruct { // no helper methods
6363
char getCharFromAnonymousStruct(struct structWithAnonymousStruct *s);
6464

6565
char getIntFromAnonymousStruct(struct structWithAnonymousStruct *s);
66+
67+
enum struct_op { STRUCT_SET, STRUCT_TEST };
68+
69+
int struct_test_long(struct bigStruct *s, enum struct_op op, long value);
70+
int struct_test_double(struct bigStruct *s, enum struct_op op, double value);
71+
int struct_test_point(struct bigStruct *s, enum struct_op op,
72+
struct point *value);

tests/samples/Struct.scala

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@ object Struct {
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]]
1616
type struct_packedStruct = native.CStruct1[native.CChar]
17+
type enum_struct_op = native.CUnsignedInt
1718
def setPoints(points: native.Ptr[struct_points], x1: native.CInt, y1: native.CInt, x2: native.CInt, y2: native.CInt): Unit = native.extern
1819
def getPoint(points: native.Ptr[struct_points], pointIndex: enum_pointIndex): native.CInt = native.extern
1920
def createPoint(): native.Ptr[struct_point] = native.extern
2021
def getBigStructSize(): native.CInt = native.extern
2122
def getCharFromAnonymousStruct(s: native.Ptr[struct_structWithAnonymousStruct]): native.CChar = native.extern
2223
def getIntFromAnonymousStruct(s: native.Ptr[struct_structWithAnonymousStruct]): native.CChar = native.extern
24+
def struct_test_long(s: native.Ptr[struct_bigStruct], op: enum_struct_op, value: native.CLong): native.CInt = native.extern
25+
def struct_test_double(s: native.Ptr[struct_bigStruct], op: enum_struct_op, value: native.CDouble): native.CInt = native.extern
26+
def struct_test_point(s: native.Ptr[struct_bigStruct], op: enum_struct_op, value: native.Ptr[struct_point]): native.CInt = native.extern
2327
}
2428

2529
import Struct._
@@ -29,6 +33,9 @@ object StructEnums {
2933
final val enum_pointIndex_Y1: enum_pointIndex = 1.toUInt
3034
final val enum_pointIndex_X2: enum_pointIndex = 2.toUInt
3135
final val enum_pointIndex_Y2: enum_pointIndex = 3.toUInt
36+
37+
final val enum_struct_op_STRUCT_SET: enum_struct_op = 0.toUInt
38+
final val enum_struct_op_STRUCT_TEST: enum_struct_op = 1.toUInt
3239
}
3340

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

103110
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)