From fa225b1b6e74e6c376b0428fdfb1f4a9c9435e37 Mon Sep 17 00:00:00 2001 From: kornilova-l Date: Sat, 16 Jun 2018 11:07:39 +0400 Subject: [PATCH] Fix tests for unions --- tests/samples/Union.c | 8 +++----- tests/samples/Union.h | 10 ++++++---- tests/samples/Union.scala | 15 ++++++++------- .../scalanative/bindgen/samples/UnionTests.scala | 9 +++++++-- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/tests/samples/Union.c b/tests/samples/Union.c index 0358aea..8cb4d02 100644 --- a/tests/samples/Union.c +++ b/tests/samples/Union.c @@ -1,8 +1,6 @@ #include "Union.h" #include -union values *getValues() { - union values *myValues = malloc(sizeof(union values)); - myValues->a = 10; - return myValues; -} +void setIntValue(union values *v) { v->i = 10; } + +void setLongValue(union values *v) { v->l = 10000000000; } diff --git a/tests/samples/Union.h b/tests/samples/Union.h index f30fef6..e3a8f95 100644 --- a/tests/samples/Union.h +++ b/tests/samples/Union.h @@ -1,7 +1,9 @@ union values { - long a; - int b; - long long c; + long l; + int i; + long long ll; }; -union values *getValues(); +void setIntValue(union values *v); + +void setLongValue(union values *v); diff --git a/tests/samples/Union.scala b/tests/samples/Union.scala index ee97f72..c9d9b6d 100644 --- a/tests/samples/Union.scala +++ b/tests/samples/Union.scala @@ -7,7 +7,8 @@ import scala.scalanative.native._ @native.extern object Union { type union_values = native.CArray[Byte, native.Nat._8] - def getValues(): native.Ptr[union_values] = native.extern + def setIntValue(v: native.Ptr[union_values]): Unit = native.extern + def setLongValue(v: native.Ptr[union_values]): Unit = native.extern } import Union._ @@ -15,11 +16,11 @@ import Union._ object UnionHelpers { implicit class union_values_pos(val p: native.Ptr[union_values]) 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 + 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 } } diff --git a/tests/samples/src/test/scala/org/scalanative/bindgen/samples/UnionTests.scala b/tests/samples/src/test/scala/org/scalanative/bindgen/samples/UnionTests.scala index aa4f025..80d6b5e 100644 --- a/tests/samples/src/test/scala/org/scalanative/bindgen/samples/UnionTests.scala +++ b/tests/samples/src/test/scala/org/scalanative/bindgen/samples/UnionTests.scala @@ -7,8 +7,13 @@ import org.scalanative.bindgen.samples.UnionHelpers._ object UnionTests extends TestSuite { val tests = Tests { 'getValues - { - val point = Union.getValues() - assert(point.a == 10) + Zone {implicit zone => + val structPtr = alloc[Union.union_values] + Union.setIntValue(structPtr) + assert(!structPtr.i == 10) + Union.setLongValue(structPtr) + assert(!structPtr.l == 10000000000L) + } } } }