Skip to content

Commit 5d35f1b

Browse files
committed
Test helper methods for inner anonymous struct
1 parent b8bad83 commit 5d35f1b

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

tests/samples/Struct.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ point_s getPoint() {
99
}
1010

1111
int getBigStructSize() { return sizeof(struct bigStruct); }
12+
13+
char getCharFromAnonymousStruct(struct structWithAnonymousStruct *s) {
14+
return s->anonymousStruct.c;
15+
}
16+
17+
char getIntFromAnonymousStruct(struct structWithAnonymousStruct *s) {
18+
return s->anonymousStruct.i;
19+
}

tests/samples/Struct.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,15 @@ struct bigStruct {
3434
};
3535

3636
int getBigStructSize();
37+
38+
struct structWithAnonymousStruct {
39+
int a;
40+
struct {
41+
char c;
42+
int i;
43+
} anonymousStruct;
44+
};
45+
46+
char getCharFromAnonymousStruct(struct structWithAnonymousStruct *s);
47+
48+
char getIntFromAnonymousStruct(struct structWithAnonymousStruct *s);

tests/samples/Struct.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ object Struct {
99
type struct_point = native.CStruct2[native.CInt, native.CInt]
1010
type point_s = native.Ptr[struct_point]
1111
type struct_bigStruct = native.CArray[Byte, native.Nat.Digit[native.Nat._1, native.Nat.Digit[native.Nat._1, native.Nat._2]]]
12+
type struct_structWithAnonymousStruct = native.CStruct2[native.CInt, native.CArray[Byte, native.Nat._8]]
1213
def getPoint(): native.Ptr[struct_point] = native.extern
1314
def getBigStructSize(): native.CInt = native.extern
15+
def getCharFromAnonymousStruct(s: native.Ptr[struct_structWithAnonymousStruct]): native.CChar = native.extern
16+
def getIntFromAnonymousStruct(s: native.Ptr[struct_structWithAnonymousStruct]): native.CChar = native.extern
1417
}
1518

1619
import Struct._
@@ -25,4 +28,13 @@ object StructHelpers {
2528
}
2629

2730
def struct_point()(implicit z: native.Zone): native.Ptr[struct_point] = native.alloc[struct_point]
31+
32+
implicit class struct_structWithAnonymousStruct_ops(val p: native.Ptr[struct_structWithAnonymousStruct]) extends AnyVal {
33+
def a: native.CInt = !p._1
34+
def a_=(value: native.CInt):Unit = !p._1 = value
35+
def anonymousStruct: native.CArray[Byte, native.Nat._8] = !p._2
36+
def anonymousStruct_=(value: native.CArray[Byte, native.Nat._8]):Unit = !p._2 = value
37+
}
38+
39+
def struct_structWithAnonymousStruct()(implicit z: native.Zone): native.Ptr[struct_structWithAnonymousStruct] = native.alloc[struct_structWithAnonymousStruct]
2840
}
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.scalanative.bindgen.samples
22

33
import utest._
4-
import scala.scalanative.native.sizeof
4+
import scala.scalanative.native._
55
import org.scalanative.bindgen.samples.StructHelpers._
66

77
object StructTests extends TestSuite {
@@ -10,10 +10,31 @@ object StructTests extends TestSuite {
1010
val point = Struct.getPoint()
1111
assert(point.x == 10)
1212
assert(point.y == 20)
13+
14+
point.x_=(11)
15+
assert(point.x == 11)
1316
}
1417

1518
'bigStructSize - {
1619
assert(Struct.getBigStructSize() == sizeof[Struct.struct_bigStruct])
1720
}
21+
22+
'innerAnonymousStruct - {
23+
type struct_anonymousStruct = CStruct2[CChar, CInt]
24+
Zone { implicit zone =>
25+
val anonymousStruct: Ptr[struct_anonymousStruct] = alloc[struct_anonymousStruct]
26+
!anonymousStruct._1 = 'a'
27+
!anonymousStruct._2 = 42
28+
29+
val structWithAnonymousStruct = struct_structWithAnonymousStruct()
30+
val array: Ptr[CArray[Byte, Nat._8]] = anonymousStruct.cast[Ptr[CArray[Byte, Nat._8]]]
31+
!structWithAnonymousStruct._2 = !array // works
32+
// structWithAnonymousStruct.anonymousStruct_=(!array) // fixme: fails
33+
// val s = structWithAnonymousStruct.anonymousStruct // fixme: fails
34+
35+
assert('a' == Struct.getCharFromAnonymousStruct(structWithAnonymousStruct))
36+
assert(42 == Struct.getIntFromAnonymousStruct(structWithAnonymousStruct))
37+
}
38+
}
1839
}
1940
}

0 commit comments

Comments
 (0)