Skip to content

Commit ef1472c

Browse files
authored
Merge pull request #97 from kornilova-l/test-union-setters
Test union setters
2 parents fdb7379 + 4d44a06 commit ef1472c

File tree

4 files changed

+178
-17
lines changed

4 files changed

+178
-17
lines changed

tests/samples/Union.c

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,55 @@
11
#include "Union.h"
22
#include <stdlib.h>
3+
#include <string.h>
34

4-
void setIntValue(union values *v) { v->i = 10; }
5+
int union_get_sizeof() { return sizeof(union values); }
56

6-
void setLongValue(union values *v) { v->l = 10000000000; }
7+
int union_test_int(union values *v, enum union_op op, int value) {
8+
switch (op) {
9+
case UNION_SET:
10+
v->i = value;
11+
return 1;
12+
case UNION_TEST:
13+
return v->i == value;
14+
}
15+
}
716

8-
int getUnionSize() { return sizeof(union values); }
17+
int union_test_long(union values *v, enum union_op op, long value) {
18+
switch (op) {
19+
case UNION_SET:
20+
v->l = value;
21+
return 1;
22+
case UNION_TEST:
23+
return v->l == value;
24+
}
25+
}
26+
27+
int union_test_long_long(union values *v, enum union_op op, long long value) {
28+
switch (op) {
29+
case UNION_SET:
30+
v->ll = value;
31+
return 1;
32+
case UNION_TEST:
33+
return v->ll == value;
34+
}
35+
}
36+
37+
int union_test_double(union values *v, enum union_op op, double value) {
38+
switch (op) {
39+
case UNION_SET:
40+
v->d = value;
41+
return 1;
42+
case UNION_TEST:
43+
return v->d == value;
44+
}
45+
}
46+
47+
int union_test_string(union values *v, enum union_op op, const char *value) {
48+
switch (op) {
49+
case UNION_SET:
50+
v->s = value;
51+
return 1;
52+
case UNION_TEST:
53+
return v->s == value || !strcmp(v->s, value);
54+
}
55+
}

tests/samples/Union.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ union values {
22
long l;
33
int i;
44
long long ll;
5+
double d;
6+
const char *s;
57
};
68

7-
void setIntValue(union values *v);
9+
enum union_op { UNION_SET, UNION_TEST };
810

9-
void setLongValue(union values *v);
11+
int union_get_sizeof();
1012

11-
int getUnionSize();
13+
int union_test_int(union values *v, enum union_op op, int value);
14+
int union_test_long(union values *v, enum union_op op, long value);
15+
int union_test_long_long(union values *v, enum union_op op, long long value);
16+
int union_test_double(union values *v, enum union_op op, double value);
17+
int union_test_string(union values *v, enum union_op op, const char *value);

tests/samples/Union.scala

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@ import scala.scalanative.native._
77
@native.extern
88
object Union {
99
type union_values = native.CArray[Byte, native.Nat._8]
10-
def setIntValue(v: native.Ptr[union_values]): Unit = native.extern
11-
def setLongValue(v: native.Ptr[union_values]): Unit = native.extern
12-
def getUnionSize(): native.CInt = native.extern
10+
type enum_union_op = native.CUnsignedInt
11+
def union_get_sizeof(): native.CInt = native.extern
12+
def union_test_int(v: native.Ptr[union_values], op: enum_union_op, value: native.CInt): native.CInt = native.extern
13+
def union_test_long(v: native.Ptr[union_values], op: enum_union_op, value: native.CLong): native.CInt = native.extern
14+
def union_test_long_long(v: native.Ptr[union_values], op: enum_union_op, value: native.CLongLong): native.CInt = native.extern
15+
def union_test_double(v: native.Ptr[union_values], op: enum_union_op, value: native.CDouble): native.CInt = native.extern
16+
def union_test_string(v: native.Ptr[union_values], op: enum_union_op, value: native.CString): native.CInt = native.extern
1317
}
1418

1519
import Union._
1620

21+
object UnionEnums {
22+
final val enum_union_op_UNION_SET: enum_union_op = 0.toUInt
23+
final val enum_union_op_UNION_TEST: enum_union_op = 1.toUInt
24+
}
25+
1726
object UnionHelpers {
1827

1928
implicit class union_values_pos(val p: native.Ptr[union_values]) extends AnyVal {
@@ -23,5 +32,9 @@ object UnionHelpers {
2332
def i_=(value: native.CInt): Unit = !p.cast[native.Ptr[native.CInt]] = value
2433
def ll: native.Ptr[native.CLongLong] = p.cast[native.Ptr[native.CLongLong]]
2534
def ll_=(value: native.CLongLong): Unit = !p.cast[native.Ptr[native.CLongLong]] = value
35+
def d: native.Ptr[native.CDouble] = p.cast[native.Ptr[native.CDouble]]
36+
def d_=(value: native.CDouble): Unit = !p.cast[native.Ptr[native.CDouble]] = value
37+
def s: native.Ptr[native.CString] = p.cast[native.Ptr[native.CString]]
38+
def s_=(value: native.CString): Unit = !p.cast[native.Ptr[native.CString]] = value
2639
}
2740
}

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

Lines changed: 103 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,113 @@ import org.scalanative.bindgen.samples.UnionHelpers._
66

77
object UnionTests extends TestSuite {
88
val tests = Tests {
9-
'getValues - {
10-
Zone {implicit zone =>
9+
'sizeof - {
10+
assert(Union.union_get_sizeof() == sizeof[Union.union_values])
11+
}
12+
13+
'get - {
14+
Zone { implicit zone =>
1115
val unionPtr = alloc[Union.union_values]
12-
Union.setIntValue(unionPtr)
13-
assert(!unionPtr.i == 10)
14-
Union.setLongValue(unionPtr)
15-
assert(!unionPtr.l == 10000000000L)
16+
17+
for (value <- Seq(Int.MinValue, -1, 0, 1, Int.MaxValue)) {
18+
Union.union_test_int(unionPtr,
19+
UnionEnums.enum_union_op_UNION_SET,
20+
value)
21+
assert(!unionPtr.i == value)
22+
}
23+
24+
for (value <- Seq(Long.MinValue, -1, 0, 1, Long.MaxValue)) {
25+
Union.union_test_long(unionPtr,
26+
UnionEnums.enum_union_op_UNION_SET,
27+
value)
28+
assert(!unionPtr.l == value)
29+
}
30+
31+
val llValues = Seq[CLongLong](
32+
Long.MinValue - 100, -1, 0, 1, Long.MaxValue + 100)
33+
for (value <- llValues) {
34+
Union.union_test_long_long(unionPtr,
35+
UnionEnums.enum_union_op_UNION_SET,
36+
value)
37+
assert(!unionPtr.ll == value)
38+
}
39+
40+
for (value <- Seq(Double.MinValue, -1, 0, 1, Double.MaxValue)) {
41+
Union.union_test_double(unionPtr,
42+
UnionEnums.enum_union_op_UNION_SET,
43+
value)
44+
assert(!unionPtr.d == value)
45+
}
46+
47+
for (value <- Seq("", "bindgen")) {
48+
val s = toCString(value)
49+
Union.union_test_string(unionPtr,
50+
UnionEnums.enum_union_op_UNION_SET,
51+
s)
52+
assert(fromCString(!unionPtr.s) == value)
53+
}
54+
55+
Union.union_test_string(unionPtr,
56+
UnionEnums.enum_union_op_UNION_SET,
57+
null)
58+
assert(!unionPtr.s == null)
1659
}
1760
}
1861

19-
'unionSize - {
20-
assert(Union.getUnionSize() == sizeof[Union.union_values])
62+
'set - {
63+
Zone { implicit zone =>
64+
val unionPtr = alloc[Union.union_values]
65+
66+
for (value <- Seq(Int.MinValue, -1, 0, 1, Int.MaxValue)) {
67+
!unionPtr.i = value
68+
assert(
69+
Union.union_test_int(unionPtr,
70+
UnionEnums.enum_union_op_UNION_TEST,
71+
value) == 1)
72+
}
73+
74+
for (value <- Seq(Long.MinValue, -1, 0, 1, Long.MaxValue)) {
75+
!unionPtr.l = value
76+
assert(
77+
Union.union_test_long(unionPtr,
78+
UnionEnums.enum_union_op_UNION_TEST,
79+
value) == 1)
80+
}
81+
82+
val llValues = Seq[CLongLong](
83+
Long.MinValue - 100, -1, 0, 1, Long.MaxValue + 100)
84+
for (value <- llValues) {
85+
!unionPtr.ll = value
86+
assert(
87+
Union.union_test_long_long(unionPtr,
88+
UnionEnums.enum_union_op_UNION_TEST,
89+
value) == 1)
90+
}
91+
92+
for (value <- Seq(Double.MinValue, -1, 0, 1, Double.MaxValue)) {
93+
!unionPtr.d = value
94+
assert(
95+
Union.union_test_double(unionPtr,
96+
UnionEnums.enum_union_op_UNION_TEST,
97+
value) == 1)
98+
}
99+
100+
for (value <- Seq("", "bindgen")) {
101+
val s = toCString(value)
102+
!unionPtr.s = s
103+
val expected = toCString(value)
104+
assert(
105+
Union.union_test_string(unionPtr,
106+
UnionEnums.enum_union_op_UNION_TEST,
107+
expected) == 1)
108+
}
109+
110+
!unionPtr.s = null
111+
assert(
112+
Union.union_test_string(unionPtr,
113+
UnionEnums.enum_union_op_UNION_TEST,
114+
null) == 1)
115+
}
21116
}
22117
}
23118
}

0 commit comments

Comments
 (0)