NativeClientSupportTest.scala 2.67 KB
Newer Older
1 2 3
package uvm.refimpl.nat

import scala.reflect.runtime.universe
Kunshan Wang's avatar
Kunshan Wang committed
4 5 6 7 8
import ch.qos.logback.classic.Level._
import uvm.refimpl.UvmBundleTesterBase
import jnr.ffi.LibraryLoader
import PlatformConstants._
import NativeSupport._
9

Kunshan Wang's avatar
Kunshan Wang committed
10 11 12 13 14
class NativeClientSupportTest extends UvmBundleTesterBase {
  setLogLevels(
    ROOT_LOGGER_NAME -> INFO,
    "uvm.refimpl.nat" -> DEBUG)

Kunshan Wang's avatar
Kunshan Wang committed
15 16
  preloadBundles("tests/uvm-refimpl-test/primitives.uir",
      "tests/uvm-refimpl-test/native-client-test.uir")
Kunshan Wang's avatar
Kunshan Wang committed
17

Kunshan Wang's avatar
Kunshan Wang committed
18 19 20 21
  val fileName = "./tests/c-snippets/ncs_tests.so"
  if (!new java.io.File(fileName).isFile()) {
    throw new RuntimeException("Need to compile the ncs_tests.so library. cd into tests/c-snippets and invoke 'make'.")
  }
Kunshan Wang's avatar
Kunshan Wang committed
22 23
  
  type CBool = Byte
Kunshan Wang's avatar
Kunshan Wang committed
24

Kunshan Wang's avatar
Kunshan Wang committed
25
  trait NcsTestsLib {
Kunshan Wang's avatar
Kunshan Wang committed
26 27 28 29
    def test_basic(mvm: Word, theID: Int, theName: String): CBool
    def test_with_ctx(mvm: Word, theID: Int, theName: String): CBool
    def test_basic_conv(mvm: Word): CBool
    def test_global_vars(mvm: Word, the_plus_one_fp: Word): CBool
Kunshan Wang's avatar
Kunshan Wang committed
30
    def test_traps(mvm: Word): CBool
Kunshan Wang's avatar
Kunshan Wang committed
31
  }
Kunshan Wang's avatar
Kunshan Wang committed
32

Kunshan Wang's avatar
Kunshan Wang committed
33
  val ncs_tests = LibraryLoader.create(classOf[NcsTestsLib]).load(fileName)
34

Kunshan Wang's avatar
Kunshan Wang committed
35
  val microVMFuncTableAddr = NativeClientSupport.exposeMicroVM(microVM)
Kunshan Wang's avatar
Kunshan Wang committed
36 37 38 39 40 41
  
  def assertNativeSuccess(result: CBool): Unit = {
    if (result == 0) {
      fail("Failed in the native program.")
    }
  }
Kunshan Wang's avatar
Kunshan Wang committed
42

Kunshan Wang's avatar
Kunshan Wang committed
43
  behavior of "The ClientAccessibleClassExposer"
Kunshan Wang's avatar
Kunshan Wang committed
44

Kunshan Wang's avatar
Kunshan Wang committed
45 46
  it should "be able to access the exposed MicroVM" in {
    val funcTablePtr = jnrMemoryManager.newPointer(microVMFuncTableAddr)
Kunshan Wang's avatar
Kunshan Wang committed
47
    val header = funcTablePtr.getAddress(0)
Kunshan Wang's avatar
Kunshan Wang committed
48 49
    val mvm = NativeClientSupport.microVMs.get(jnrMemoryManager.newPointer(header))

Kunshan Wang's avatar
Kunshan Wang committed
50
    mvm shouldBe microVM
Kunshan Wang's avatar
Kunshan Wang committed
51 52 53 54

    val theName = "@i64"
    val theID = microVM.idOf(theName)

Kunshan Wang's avatar
Kunshan Wang committed
55
    val result = ncs_tests.test_basic(microVMFuncTableAddr, theID, theName)
Kunshan Wang's avatar
Kunshan Wang committed
56
    assertNativeSuccess(result)
Kunshan Wang's avatar
Kunshan Wang committed
57 58
  }

Kunshan Wang's avatar
Kunshan Wang committed
59
  it should "be able to create MuCtx and use it" in {
Kunshan Wang's avatar
Kunshan Wang committed
60 61 62 63
    val theName = "@double"
    val theID = microVM.idOf(theName)

    val result = ncs_tests.test_with_ctx(microVMFuncTableAddr, theID, theName)
Kunshan Wang's avatar
Kunshan Wang committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    assertNativeSuccess(result)
  }
  
  it should "convert C types to Mu types and back, with the same value" in {
    val result = ncs_tests.test_basic_conv(microVMFuncTableAddr)
    assertNativeSuccess(result)
  }
  
  it should "get values from global variables" in {
    val ctx = microVM.newContext()
    val hThePlusOneFP = ctx.handleFromExpose(ctx.idOf("@plus_one_native"))
    val thePlusOneFP = ctx.handleToFP(hThePlusOneFP)
    ctx.closeContext()
    
    val result = ncs_tests.test_global_vars(microVMFuncTableAddr, thePlusOneFP)
    assertNativeSuccess(result)
80
  }
Kunshan Wang's avatar
Kunshan Wang committed
81 82 83 84 85
  
  it should "support traps" in {
    val result = ncs_tests.test_traps(microVMFuncTableAddr)
    assertNativeSuccess(result)
  }
86
}