NativeClientSupportTest.scala 1.82 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 15
class NativeClientSupportTest extends UvmBundleTesterBase {
  setLogLevels(
    ROOT_LOGGER_NAME -> INFO,
    "uvm.refimpl.nat" -> DEBUG)

  preloadBundles("tests/uvm-refimpl-test/primitives.uir")
16

Kunshan Wang's avatar
Kunshan Wang committed
17 18 19 20
  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'.")
  }
21

Kunshan Wang's avatar
Kunshan Wang committed
22
  trait NcsTestsLib {
23
    def test_basic(mvm: Word, theID: Int, theName: String): Int
24
    def test_with_ctx(mvm: Word, theID: Int, theName: String): Int
Kunshan Wang's avatar
Kunshan Wang committed
25
  }
26

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

29
  val microVMFuncTableAddr = NativeClientSupport.exposeMicroVM(microVM)
30

31 32 33 34
  behavior of "The ClientAccessibleClassExposer"
  
  it should "be able to access the exposed MicroVM" in {
    val funcTablePtr = jnrMemoryManager.newPointer(microVMFuncTableAddr)
Kunshan Wang's avatar
Kunshan Wang committed
35
    val header = funcTablePtr.getAddress(0)
36 37
    val mvm = NativeClientSupport.microVMs.get(jnrMemoryManager.newPointer(header))

Kunshan Wang's avatar
Kunshan Wang committed
38
    mvm shouldBe microVM
39 40 41 42

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

43 44 45 46 47 48 49 50 51 52 53 54 55
    val result = ncs_tests.test_basic(microVMFuncTableAddr, theID, theName)
    if (result == 0) {
      fail("Failed in the native program.")
    }
  }
    
  it should "be able to create MuCtx and use it" in {
    val funcTablePtr = jnrMemoryManager.newPointer(microVMFuncTableAddr)

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

    val result = ncs_tests.test_with_ctx(microVMFuncTableAddr, theID, theName)
56 57 58
    if (result == 0) {
      fail("Failed in the native program.")
    }
59 60
  }
}