Skip to content

Commit

Permalink
test: testing fake.ksy (#24)
Browse files Browse the repository at this point in the history
* test: improve makefile to compile with patterns

* test: unit test for fake.ksy

* test: generate list of libraries dynamically

* feat: only compile current cpp file

this is needed if we have multiple libraries in the same folder.
see
#24 (comment)

However when we implement `import`
#17
we will probably need to revisit again

* test: compare the actual array
  • Loading branch information
zonca authored Jan 10, 2024
1 parent f87021d commit 953b889
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
17 changes: 9 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@ ifneq (,$(wildcard /proc/sys/fs/binfmt_misc/WSLInterop))
else
JAR_PATH = /usr/share/kaitai-struct-compiler/lib/*
endif
all: src-animal/libanimal.so
KSY := animal fake
LIBS := $(foreach ksy,$(KSY),test_artifacts/lib$(ksy).so)

src-animal/libanimal.so: $(BUILD) src-animal/animal.cpp
PYTHONPATH=$$PYTHONPATH:local $(BUILD) src-animal/animal.cpp -b build
test_artifacts/lib%.so: test_artifacts/%.cpp $(BUILD)
PYTHONPATH=$$PYTHONPATH:local $(BUILD) $< -b build

$(BUILD):
pip install . -t local

src-animal/animal.cpp: $(JAVA_CLASSES)
java -cp kaitai_struct_compiler/jvm/target/scala-2.12/kaitai-struct-compiler_2.12-0.11-SNAPSHOT.jar:$(JAR_PATH) io.kaitai.struct.JavaMain -t awkward --outdir src-animal example_data/schemas/animal.ksy
test_artifacts/%.cpp: example_data/schemas/%.ksy $(JAVA_CLASSES)
java -cp kaitai_struct_compiler/jvm/target/scala-2.12/kaitai-struct-compiler_2.12-0.11-SNAPSHOT.jar:$(JAR_PATH) io.kaitai.struct.JavaMain -t awkward --outdir test_artifacts $<

$(JAVA_CLASSES):
cd kaitai_struct_compiler && sbt package

test: src-animal/libanimal.so
test: $(LIBS)
pytest

list:
@LC_ALL=C $(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$'
@LC_ALL=C $(MAKE) -pRrq -f $(lastword $(MAKEFILE_LIST)) : 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | sort | egrep -v -e '^[^[:alnum:]]' -e '^$@$$'

clean:
rm -rf local src-animal && cd kaitai_struct_compiler && rm -fr js/target/ jvm/target/ project/project/ project/target/ target/ .bsp/
rm -rf local test_artifacts && cd kaitai_struct_compiler && rm -fr js/target/ jvm/target/ project/project/ project/target/ target/ .bsp/
2 changes: 1 addition & 1 deletion src/awkward_kaitai/data/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ if(NOT awkward-headers_POPULATED)
EXCLUDE_FROM_ALL)
endif()

file(GLOB KAITAI_SOURCE_FILES CONFIGURE_DEPENDS "${KAITAI_DIR}/*.cpp")
file(GLOB KAITAI_SOURCE_FILES CONFIGURE_DEPENDS "${KAITAI_DIR}/${KAITAI_NAME}.cpp")

# Create a shared library
add_library("${KAITAI_NAME}" SHARED ${KAITAI_SOURCE_FILES})
Expand Down
2 changes: 1 addition & 1 deletion tests/test_animal.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def test_animal():
animal = awkward_kaitai.Reader(
"src-animal/libanimal.so"
"test_artifacts/libanimal.so"
) # pass the path of the shared file
awkward_array = animal.load("example_data/data/animal.raw")

Expand Down
26 changes: 26 additions & 0 deletions tests/test_fake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from __future__ import annotations

import sys

import numpy as np

sys.path.append("local")
import awkward_kaitai


def test_fake():
reader = awkward_kaitai.Reader("test_artifacts/libfake.so")
awkward_array = reader.load("example_data/data/fake.raw")

assert len(awkward_array.fakeA__Zpoints.pointA__Zx[0]) == 3000
assert awkward_array.fakeA__Zpoints.pointA__Zy[0][0] == 2
assert awkward_array.fakeA__Zpoints.pointA__Zz.to_numpy().sum() == 66000

expected_array = np.array(
[(1, 2, 3), (10, 20, 30), (11, 22, 33)] * 1000,
dtype=[("pointA__Zx", "u4"), ("pointA__Zy", "u4"), ("pointA__Zz", "u4")],
)

np.testing.assert_array_equal(
awkward_array.fakeA__Zpoints.to_numpy()[0], expected_array
)

0 comments on commit 953b889

Please sign in to comment.