Skip to content

Commit b8bad83

Browse files
committed
Add tests for types that use native.CArray
1 parent 2f57db1 commit b8bad83

File tree

10 files changed

+49
-5
lines changed

10 files changed

+49
-5
lines changed

bindgen/ir/IR.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &s, const IR &ir) {
141141
s << "object " << ir.libName << "Helpers {\n";
142142

143143
for (const auto &st : ir.structs) {
144-
s << "\n" << st->generateHelperClass();
144+
if (st->hasHelperMethods()) {
145+
s << "\n" << st->generateHelperClass();
146+
}
145147
}
146148

147149
for (const auto &u : ir.unions) {

bindgen/ir/Struct.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ std::shared_ptr<TypeDef> Struct::generateTypeDef() {
3737
}
3838

3939
std::string Struct::generateHelperClass() const {
40-
if (!hasHelperMethods()) {
41-
/* struct is empty or represented as an array */
42-
return "";
43-
}
40+
assert(hasHelperMethods());
41+
/* struct is not empty and not represented as an array */
4442
std::stringstream s;
4543
std::string type = getAliasType();
4644
s << " implicit class " << type << "_ops(val p: native.Ptr[" << type

tests/samples/Struct.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ point_s getPoint() {
77
point->y = 20;
88
return point;
99
}
10+
11+
int getBigStructSize() { return sizeof(struct bigStruct); }

tests/samples/Struct.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,31 @@ struct point {
66
typedef struct point *point_s;
77

88
point_s getPoint();
9+
10+
struct bigStruct {
11+
long one;
12+
char two;
13+
int three;
14+
float four;
15+
double five;
16+
point_s six;
17+
int seven;
18+
int eight;
19+
int nine;
20+
int ten;
21+
int eleven;
22+
int twelve;
23+
int thirteen;
24+
int fourteen;
25+
int fifteen;
26+
int sixteen;
27+
int seventeen;
28+
int eighteen;
29+
int nineteen;
30+
int twenty;
31+
int twentyOne;
32+
int twentyTwo;
33+
int twentyThree;
34+
};
35+
36+
int getBigStructSize();

tests/samples/Struct.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import scala.scalanative.native._
88
object Struct {
99
type struct_point = native.CStruct2[native.CInt, native.CInt]
1010
type point_s = native.Ptr[struct_point]
11+
type struct_bigStruct = native.CArray[Byte, native.Nat.Digit[native.Nat._1, native.Nat.Digit[native.Nat._1, native.Nat._2]]]
1112
def getPoint(): native.Ptr[struct_point] = native.extern
13+
def getBigStructSize(): native.CInt = native.extern
1214
}
1315

1416
import Struct._

tests/samples/Union.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
void setIntValue(union values *v) { v->i = 10; }
55

66
void setLongValue(union values *v) { v->l = 10000000000; }
7+
8+
int getUnionSize() { return sizeof(union values); }

tests/samples/Union.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ union values {
77
void setIntValue(union values *v);
88

99
void setLongValue(union values *v);
10+
11+
int getUnionSize();

tests/samples/Union.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ object Union {
99
type union_values = native.CArray[Byte, native.Nat._8]
1010
def setIntValue(v: native.Ptr[union_values]): Unit = native.extern
1111
def setLongValue(v: native.Ptr[union_values]): Unit = native.extern
12+
def getUnionSize(): native.CInt = native.extern
1213
}
1314

1415
import Union._

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.scalanative.bindgen.samples
22

33
import utest._
4+
import scala.scalanative.native.sizeof
45
import org.scalanative.bindgen.samples.StructHelpers._
56

67
object StructTests extends TestSuite {
@@ -10,5 +11,9 @@ object StructTests extends TestSuite {
1011
assert(point.x == 10)
1112
assert(point.y == 20)
1213
}
14+
15+
'bigStructSize - {
16+
assert(Struct.getBigStructSize() == sizeof[Struct.struct_bigStruct])
17+
}
1318
}
1419
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ object UnionTests extends TestSuite {
1313
assert(!structPtr.i == 10)
1414
Union.setLongValue(structPtr)
1515
assert(!structPtr.l == 10000000000L)
16+
17+
assert(Union.getUnionSize() == sizeof[Union.union_values])
1618
}
1719
}
1820
}

0 commit comments

Comments
 (0)