Skip to content

Commit e857065

Browse files
authored
Merge pull request #56 from kornilova-l/end-to-end-tests-for-unions
[WIP] End to end tests for unions
2 parents 5c1b35e + fa225b1 commit e857065

File tree

7 files changed

+47
-15
lines changed

7 files changed

+47
-15
lines changed

bindgen/visitor/TreeVisitor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ void TreeVisitor::handleUnion(clang::RecordDecl *record, std::string name) {
108108
std::vector<Field> fields;
109109

110110
for (const clang::FieldDecl *field : record->fields()) {
111-
maxSize = std::max(maxSize, astContext->getTypeSize(field->getType()));
111+
uint64_t sizeInBytes = astContext->getTypeSize(field->getType()) / 8;
112+
maxSize = std::max(maxSize, sizeInBytes);
112113
std::string fname = field->getNameAsString();
113114
std::string ftype = handleReservedWords(
114115
typeTranslator.Translate(field->getType(), &name));

tests/samples/PrivateMembers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ object PrivateMembers {
1616
type struct_structWithPrivateStruct = native.CStruct1[native.Ptr[struct_structWithPrivateType]]
1717
type struct_normalStruct = native.CStruct1[native.CInt]
1818
type struct_privateStructWithTypedef = native.CStruct1[native.Ptr[__private_type]]
19-
type union___unionWithPrivateName = native.CArray[Byte, native.Nat.Digit[native.Nat._3, native.Nat._2]]
19+
type union___unionWithPrivateName = native.CArray[Byte, native.Nat._4]
2020
def getTypeThatUsesPrivateTypes(): pid_t = native.extern
2121
def getPrivateType(): native.Ptr[__private_type] = native.extern
2222
def usesPrivateUnion(anonymous0: union___unionWithPrivateName): Unit = native.extern

tests/samples/ReservedWords.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ object ReservedWords {
1515
type `finally` = struct_finally
1616
type struct_object = native.CStruct2[`match`, native.CInt]
1717
type struct_finally = native.CStruct2[`def`, `lazy`]
18-
type union_lazy = native.CArray[Byte, native.Nat.Digit[native.Nat._1, native.Nat.Digit[native.Nat._2, native.Nat._8]]]
18+
type union_lazy = native.CArray[Byte, native.Nat.Digit[native.Nat._1, native.Nat._6]]
1919
def `with`(`sealed`: `match`, `implicit`: native.Ptr[`match`], `forSome`: `lazy`): `type` = native.extern
2020
def `implicit`(`type`: native.Ptr[`finally`]): `match` = native.extern
2121
def _1(): Unit = native.extern

tests/samples/Union.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "Union.h"
2+
#include <stdlib.h>
3+
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: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
union point {
2-
long a;
3-
int b;
4-
long long c;
1+
union values {
2+
long l;
3+
int i;
4+
long long ll;
55
};
6+
7+
void setIntValue(union values *v);
8+
9+
void setLongValue(union values *v);

tests/samples/Union.scala

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ import scala.scalanative.native._
66
@native.link("bindgentests")
77
@native.extern
88
object Union {
9-
type union_point = native.CArray[Byte, native.Nat.Digit[native.Nat._6, native.Nat._4]]
9+
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
1012
}
1113

1214
import Union._
1315

1416
object UnionHelpers {
1517

16-
implicit class union_point_pos(val p: native.Ptr[union_point]) extends AnyVal {
17-
def a: native.Ptr[native.CLong] = p.cast[native.Ptr[native.CLong]]
18-
def a_=(value: native.CLong): Unit = !p.cast[native.Ptr[native.CLong]] = value
19-
def b: native.Ptr[native.CInt] = p.cast[native.Ptr[native.CInt]]
20-
def b_=(value: native.CInt): Unit = !p.cast[native.Ptr[native.CInt]] = value
21-
def c: native.Ptr[native.CLongLong] = p.cast[native.Ptr[native.CLongLong]]
22-
def c_=(value: native.CLongLong): Unit = !p.cast[native.Ptr[native.CLongLong]] = value
18+
implicit class union_values_pos(val p: native.Ptr[union_values]) extends AnyVal {
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
2325
}
2426
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.scalanative.bindgen.samples
2+
3+
import utest._
4+
import scala.scalanative.native._
5+
import org.scalanative.bindgen.samples.UnionHelpers._
6+
7+
object UnionTests extends TestSuite {
8+
val tests = Tests {
9+
'getValues - {
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+
}
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)