Skip to content

Commit

Permalink
Fix tests for unions
Browse files Browse the repository at this point in the history
  • Loading branch information
kornilova203 committed Jun 16, 2018
1 parent 63623b7 commit fa225b1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
8 changes: 3 additions & 5 deletions tests/samples/Union.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "Union.h"
#include <stdlib.h>

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; }
10 changes: 6 additions & 4 deletions tests/samples/Union.h
Original file line number Diff line number Diff line change
@@ -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);
15 changes: 8 additions & 7 deletions tests/samples/Union.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ 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._

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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}

0 comments on commit fa225b1

Please sign in to comment.