Skip to content

Commit fa225b1

Browse files
committed
Fix tests for unions
1 parent 63623b7 commit fa225b1

File tree

4 files changed

+24
-18
lines changed

4 files changed

+24
-18
lines changed

tests/samples/Union.c

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

4-
union values *getValues() {
5-
union values *myValues = malloc(sizeof(union values));
6-
myValues->a = 10;
7-
return myValues;
8-
}
4+
void setIntValue(union values *v) { v->i = 10; }
5+
6+
void setLongValue(union values *v) { v->l = 10000000000; }

tests/samples/Union.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
union values {
2-
long a;
3-
int b;
4-
long long c;
2+
long l;
3+
int i;
4+
long long ll;
55
};
66

7-
union values *getValues();
7+
void setIntValue(union values *v);
8+
9+
void setLongValue(union values *v);

tests/samples/Union.scala

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import scala.scalanative.native._
77
@native.extern
88
object Union {
99
type union_values = native.CArray[Byte, native.Nat._8]
10-
def getValues(): native.Ptr[union_values] = native.extern
10+
def setIntValue(v: native.Ptr[union_values]): Unit = native.extern
11+
def setLongValue(v: native.Ptr[union_values]): Unit = native.extern
1112
}
1213

1314
import Union._
1415

1516
object UnionHelpers {
1617

1718
implicit class union_values_pos(val p: native.Ptr[union_values]) extends AnyVal {
18-
def a: native.Ptr[native.CLong] = p.cast[native.Ptr[native.CLong]]
19-
def a_=(value: native.CLong): Unit = !p.cast[native.Ptr[native.CLong]] = value
20-
def b: native.Ptr[native.CInt] = p.cast[native.Ptr[native.CInt]]
21-
def b_=(value: native.CInt): Unit = !p.cast[native.Ptr[native.CInt]] = value
22-
def c: native.Ptr[native.CLongLong] = p.cast[native.Ptr[native.CLongLong]]
23-
def c_=(value: native.CLongLong): Unit = !p.cast[native.Ptr[native.CLongLong]] = value
19+
def l: native.Ptr[native.CLong] = p.cast[native.Ptr[native.CLong]]
20+
def l_=(value: native.CLong): Unit = !p.cast[native.Ptr[native.CLong]] = value
21+
def i: native.Ptr[native.CInt] = p.cast[native.Ptr[native.CInt]]
22+
def i_=(value: native.CInt): Unit = !p.cast[native.Ptr[native.CInt]] = value
23+
def ll: native.Ptr[native.CLongLong] = p.cast[native.Ptr[native.CLongLong]]
24+
def ll_=(value: native.CLongLong): Unit = !p.cast[native.Ptr[native.CLongLong]] = value
2425
}
2526
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ import org.scalanative.bindgen.samples.UnionHelpers._
77
object UnionTests extends TestSuite {
88
val tests = Tests {
99
'getValues - {
10-
val point = Union.getValues()
11-
assert(point.a == 10)
10+
Zone {implicit zone =>
11+
val structPtr = alloc[Union.union_values]
12+
Union.setIntValue(structPtr)
13+
assert(!structPtr.i == 10)
14+
Union.setLongValue(structPtr)
15+
assert(!structPtr.l == 10000000000L)
16+
}
1217
}
1318
}
1419
}

0 commit comments

Comments
 (0)