test_memops.py 3.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright 2017 The Australian National University
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
from util import fncptr_from_c_script, may_spawn_proc
16 17 18
import ctypes


19
@may_spawn_proc
20
def test_uptr_bytestore_load():
John Zhang's avatar
John Zhang committed
21 22 23
    fn, _ = fncptr_from_c_script("test_uptr_bytestore_load.c", "test_fnc",
                                 argtypes=[ctypes.POINTER(ctypes.c_uint32)],
                                 restype=ctypes.c_uint32)
24 25 26 27 28

    # allocate memory through ctypes
    ui32 = ctypes.c_uint32()
    assert fn(ctypes.byref(ui32)) == 0x8d9f9c1d
    assert ui32.value == 0x8d9f9c1d
John Zhang's avatar
John Zhang committed
29

30
@may_spawn_proc
John Zhang's avatar
John Zhang committed
31 32 33 34 35 36 37 38 39 40 41
def test_getfieldiref():
    class Stt(ctypes.Structure):
        _fields_ = [('ui8', ctypes.c_uint8),
                    ('ui64', ctypes.c_uint64),
                    ('ui32', ctypes.c_uint32)]

    fn, _ = fncptr_from_c_script("test_getfieldiref.c", "test_fnc",
                                 argtypes=[ctypes.POINTER(Stt)],
                                 restype=ctypes.c_uint32)
    stt = Stt()
    stt.ui8 = 25
John Zhang's avatar
John Zhang committed
42 43
    stt.ui64 = 0xabcdef0123456789
    stt.ui32 = 0xcafebabe
John Zhang's avatar
John Zhang committed
44 45

    res = fn(ctypes.byref(stt))
John Zhang's avatar
John Zhang committed
46
    assert res == 0xcafebabe, "result: %s" % hex(res)
John Zhang's avatar
John Zhang committed
47

48
@may_spawn_proc
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
def test_getelemiref():
    Arr = ctypes.ARRAY(ctypes.c_int64, 5)
    fn, _ = fncptr_from_c_script("test_getelemiref.c", "test_fnc",
                                 argtypes=[ctypes.POINTER(Arr)],
                                 restype=ctypes.c_int64)
    arr = Arr()
    arr[0] = -23
    arr[1] = 35
    arr[2] = 42
    arr[3] = 0
    arr[4] = 152

    res = fn(ctypes.byref(arr), 2)
    assert res == 42, "result: %d" % res

64
@may_spawn_proc
John Zhang's avatar
John Zhang committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
def test_getvarpartiref():
    class Stt(ctypes.Structure):
        _fields_ = [('ui8', ctypes.c_uint8),
                    ('ui64', ctypes.c_uint64),
                    ('ui32s', ctypes.ARRAY(ctypes.c_uint32, 5))]

    fn, _ = fncptr_from_c_script("test_getvarpartiref.c", "test_fnc",
                                 argtypes=[ctypes.POINTER(Stt)],
                                 restype=ctypes.c_uint32)
    stt = Stt()
    stt.ui8 = 25
    stt.ui64 = 0xabcdef0123456789
    stt.ui32s[0] = 0xcafebabe

    res = fn(ctypes.byref(stt))
    assert res == 0xcafebabe, "result: %s" % hex(res)

82
@may_spawn_proc
John Zhang's avatar
John Zhang committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
def test_getvarpartiref_nofix():
    Arr = ctypes.ARRAY(ctypes.c_uint32, 3)

    fn, _ = fncptr_from_c_script("test_getvarpartiref_nofix.c", "test_fnc",
                                 argtypes=[ctypes.POINTER(Arr)],
                                 restype=ctypes.c_uint32)
    arr = Arr()
    arr[0] = 0xcafebabe
    arr[1] = 0xbecca
    arr[2] = 0xfaebee

    res = fn(ctypes.byref(arr))
    assert res == 0xcafebabe, "result: %s" % hex(res)