blob: 31a3bc0876ea1cbd5d86a0512a8b7b6736a32e37 [file] [log] [blame]
Diego Russo56cd4a62020-04-23 16:41:07 +01001#!/usr/bin/env python3
2# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
3#
4# SPDX-License-Identifier: Apache-2.0
5#
6# Licensed under the Apache License, Version 2.0 (the License); you may
7# not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an AS IS BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17# Simple example of the usage of mlw_codec.
18import pytest
19from ethosu import mlw_codec
20
21
22class TestMLWCodec:
23 """ This class is responsible to test the mlw_codec library
24 It mainly tests the two methods encode() and decode() with different inputs"""
25
26 weights = [0, 2, 3, 0, -1, -2, -3, 0, 0, 0, 1, -250, 240] * 3
27 compressed_weights = bytearray(
28 b"\xb8\x00\\q^\x1f\xfc\x01\x03\x05\x08\x0c\x10\x908\x12\xd7\x99:\xd2\x99$\xae#\x9d\xa9#\x00\xf0\xff\xff\xff"
29 )
30 empty_decoded = bytearray(b"\xfe\xffC\x00\xf0\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff")
31
32 # Generate parameters lists for the tests below
33 encode_testdata = [
34 (mlw_codec.encode, weights, compressed_weights),
35 pytest.param(mlw_codec.encode, ["a"], empty_decoded, marks=pytest.mark.xfail), # cannot accept strings
36 ]
37
38 decode_testdata = [(mlw_codec.decode, compressed_weights, weights)]
39
40 codec_testdata = [
41 (weights, weights),
42 ([1] * 10, [1] * 10),
43 pytest.param(["a"], ["a"], marks=pytest.mark.xfail), # cannot accept strings
44 ]
45
46 @pytest.mark.parametrize("function_under_test,test_input,expected", encode_testdata)
47 def test_mlw_codec(self, function_under_test, test_input, expected):
48 self._call_mlw_codec_method(function_under_test, test_input, expected)
49
50 @pytest.mark.parametrize("function_under_test,test_input,expected", decode_testdata)
51 def test_mlw_decode(self, function_under_test, test_input, expected):
52 self._call_mlw_codec_method(function_under_test, test_input, expected)
53
54 @pytest.mark.parametrize("test_input,expected", codec_testdata)
55 def test_mlw_encode_decode(self, test_input, expected):
56 output = mlw_codec.decode(mlw_codec.encode(test_input))
57 assert output == expected
58
59 def _call_mlw_codec_method(self, method_name, test_input, expected):
60 output = method_name(test_input)
61 assert output == expected