Skip to content

Commit

Permalink
Merge pull request #56 from kornilova-l/end-to-end-tests-for-unions
Browse files Browse the repository at this point in the history
[WIP] End to end tests for unions
  • Loading branch information
kornilova203 authored Jun 16, 2018
2 parents 5c1b35e + fa225b1 commit e857065
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 15 deletions.
3 changes: 2 additions & 1 deletion bindgen/visitor/TreeVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ void TreeVisitor::handleUnion(clang::RecordDecl *record, std::string name) {
std::vector<Field> fields;

for (const clang::FieldDecl *field : record->fields()) {
maxSize = std::max(maxSize, astContext->getTypeSize(field->getType()));
uint64_t sizeInBytes = astContext->getTypeSize(field->getType()) / 8;
maxSize = std::max(maxSize, sizeInBytes);
std::string fname = field->getNameAsString();
std::string ftype = handleReservedWords(
typeTranslator.Translate(field->getType(), &name));
Expand Down
2 changes: 1 addition & 1 deletion tests/samples/PrivateMembers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object PrivateMembers {
type struct_structWithPrivateStruct = native.CStruct1[native.Ptr[struct_structWithPrivateType]]
type struct_normalStruct = native.CStruct1[native.CInt]
type struct_privateStructWithTypedef = native.CStruct1[native.Ptr[__private_type]]
type union___unionWithPrivateName = native.CArray[Byte, native.Nat.Digit[native.Nat._3, native.Nat._2]]
type union___unionWithPrivateName = native.CArray[Byte, native.Nat._4]
def getTypeThatUsesPrivateTypes(): pid_t = native.extern
def getPrivateType(): native.Ptr[__private_type] = native.extern
def usesPrivateUnion(anonymous0: union___unionWithPrivateName): Unit = native.extern
Expand Down
2 changes: 1 addition & 1 deletion tests/samples/ReservedWords.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object ReservedWords {
type `finally` = struct_finally
type struct_object = native.CStruct2[`match`, native.CInt]
type struct_finally = native.CStruct2[`def`, `lazy`]
type union_lazy = native.CArray[Byte, native.Nat.Digit[native.Nat._1, native.Nat.Digit[native.Nat._2, native.Nat._8]]]
type union_lazy = native.CArray[Byte, native.Nat.Digit[native.Nat._1, native.Nat._6]]
def `with`(`sealed`: `match`, `implicit`: native.Ptr[`match`], `forSome`: `lazy`): `type` = native.extern
def `implicit`(`type`: native.Ptr[`finally`]): `match` = native.extern
def _1(): Unit = native.extern
Expand Down
6 changes: 6 additions & 0 deletions tests/samples/Union.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "Union.h"
#include <stdlib.h>

void setIntValue(union values *v) { v->i = 10; }

void setLongValue(union values *v) { v->l = 10000000000; }
12 changes: 8 additions & 4 deletions tests/samples/Union.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
union point {
long a;
int b;
long long c;
union values {
long l;
int i;
long long ll;
};

void setIntValue(union values *v);

void setLongValue(union values *v);
18 changes: 10 additions & 8 deletions tests/samples/Union.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@ import scala.scalanative.native._
@native.link("bindgentests")
@native.extern
object Union {
type union_point = native.CArray[Byte, native.Nat.Digit[native.Nat._6, native.Nat._4]]
type union_values = native.CArray[Byte, native.Nat._8]
def setIntValue(v: native.Ptr[union_values]): Unit = native.extern
def setLongValue(v: native.Ptr[union_values]): Unit = native.extern
}

import Union._

object UnionHelpers {

implicit class union_point_pos(val p: native.Ptr[union_point]) extends AnyVal {
def a: native.Ptr[native.CLong] = p.cast[native.Ptr[native.CLong]]
def a_=(value: native.CLong): Unit = !p.cast[native.Ptr[native.CLong]] = value
def b: native.Ptr[native.CInt] = p.cast[native.Ptr[native.CInt]]
def b_=(value: native.CInt): Unit = !p.cast[native.Ptr[native.CInt]] = value
def c: native.Ptr[native.CLongLong] = p.cast[native.Ptr[native.CLongLong]]
def c_=(value: native.CLongLong): Unit = !p.cast[native.Ptr[native.CLongLong]] = value
implicit class union_values_pos(val p: native.Ptr[union_values]) extends AnyVal {
def l: native.Ptr[native.CLong] = p.cast[native.Ptr[native.CLong]]
def l_=(value: native.CLong): Unit = !p.cast[native.Ptr[native.CLong]] = value
def i: native.Ptr[native.CInt] = p.cast[native.Ptr[native.CInt]]
def i_=(value: native.CInt): Unit = !p.cast[native.Ptr[native.CInt]] = value
def ll: native.Ptr[native.CLongLong] = p.cast[native.Ptr[native.CLongLong]]
def ll_=(value: native.CLongLong): Unit = !p.cast[native.Ptr[native.CLongLong]] = value
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.scalanative.bindgen.samples

import utest._
import scala.scalanative.native._
import org.scalanative.bindgen.samples.UnionHelpers._

object UnionTests extends TestSuite {
val tests = Tests {
'getValues - {
Zone {implicit zone =>
val structPtr = alloc[Union.union_values]
Union.setIntValue(structPtr)
assert(!structPtr.i == 10)
Union.setLongValue(structPtr)
assert(!structPtr.l == 10000000000L)
}
}
}
}

0 comments on commit e857065

Please sign in to comment.