Coverage for tests/test_table.py: 100%
53 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-20 16:25 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-20 16:25 +0000
1import unittest
3from wasmtime import *
6class TestTable(unittest.TestCase):
7 def test_new(self):
8 store = Store()
9 module = Module(store.engine, """
10 (module (table (export "") 1 funcref))
11 """)
12 table = Instance(store, module, []).exports(store).by_index[0]
13 assert(isinstance(table, Table))
14 assert(isinstance(table.type(store), TableType))
15 self.assertEqual(table.type(store).limits, Limits(1, None))
16 self.assertEqual(table.size(store), 1)
18 ty = TableType(ValType.funcref(), Limits(1, 2))
19 store = Store()
20 func = Func(store, FuncType([], []), lambda: {})
21 Table(store, ty, func)
23 def test_grow(self):
24 ty = TableType(ValType.funcref(), Limits(1, 2))
25 store = Store()
26 table = Table(store, ty, None)
27 self.assertEqual(table.size(store), 1)
29 # type errors
30 with self.assertRaises(TypeError):
31 table.grow(store, 'x', None) # type: ignore
32 with self.assertRaises(TypeError):
33 table.grow(store, 2, 'x')
35 # growth works
36 table.grow(store, 1, None)
37 self.assertEqual(table.size(store), 2)
39 # can't grow beyond max
40 with self.assertRaises(WasmtimeError):
41 table.grow(store, 1, None)
43 def test_get(self):
44 ty = TableType(ValType.funcref(), Limits(1, 2))
45 store = Store()
46 table = Table(store, ty, None)
47 self.assertEqual(table.get(store, 0), None)
48 self.assertEqual(table.get(store, 1), None)
50 called = {}
51 called['hit'] = False
53 def set_called():
54 called['hit'] = True
55 func = Func(store, FuncType([], []), set_called)
56 table.grow(store, 1, func)
57 assert(not called['hit'])
58 f = table.get(store, 1)
59 assert(isinstance(f, Func))
60 f(store)
61 assert(called['hit'])
63 def test_set(self):
64 ty = TableType(ValType.funcref(), Limits(1, 2))
65 store = Store()
66 table = Table(store, ty, None)
67 func = Func(store, FuncType([], []), lambda: {})
68 table.set(store, 0, func)
69 with self.assertRaises(WasmtimeError):
70 table.set(store, 1, func)