Diego Russo | 56cd4a6 | 2020-04-23 16:41:07 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Rickard Bolin | bc6ee58 | 2022-11-04 08:24:29 +0000 | [diff] [blame] | 2 | # SPDX-FileCopyrightText: Copyright 2020-2022 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Diego Russo | 56cd4a6 | 2020-04-23 16:41:07 +0100 | [diff] [blame] | 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. |
Rickard Bolin | bc6ee58 | 2022-11-04 08:24:29 +0000 | [diff] [blame] | 17 | # |
Diego Russo | 56cd4a6 | 2020-04-23 16:41:07 +0100 | [diff] [blame] | 18 | # Simple example of the usage of mlw_codec. |
Jonas Ohlsson | 845e232 | 2022-03-01 12:39:55 +0100 | [diff] [blame] | 19 | from typing import Any |
| 20 | from typing import List |
| 21 | |
Diego Russo | 56cd4a6 | 2020-04-23 16:41:07 +0100 | [diff] [blame] | 22 | import pytest |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 23 | |
Diego Russo | 56cd4a6 | 2020-04-23 16:41:07 +0100 | [diff] [blame] | 24 | from ethosu import mlw_codec |
| 25 | |
| 26 | |
| 27 | class TestMLWCodec: |
Jonas Ohlsson | d857507 | 2022-03-30 10:30:25 +0200 | [diff] [blame] | 28 | """This class is responsible to test the mlw_codec library |
Diego Russo | 56cd4a6 | 2020-04-23 16:41:07 +0100 | [diff] [blame] | 29 | It mainly tests the two methods encode() and decode() with different inputs""" |
| 30 | |
| 31 | weights = [0, 2, 3, 0, -1, -2, -3, 0, 0, 0, 1, -250, 240] * 3 |
| 32 | compressed_weights = bytearray( |
| 33 | 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" |
| 34 | ) |
| 35 | empty_decoded = bytearray(b"\xfe\xffC\x00\xf0\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff") |
| 36 | |
| 37 | # Generate parameters lists for the tests below |
| 38 | encode_testdata = [ |
| 39 | (mlw_codec.encode, weights, compressed_weights), |
| 40 | pytest.param(mlw_codec.encode, ["a"], empty_decoded, marks=pytest.mark.xfail), # cannot accept strings |
| 41 | ] |
| 42 | |
| 43 | decode_testdata = [(mlw_codec.decode, compressed_weights, weights)] |
| 44 | |
| 45 | codec_testdata = [ |
| 46 | (weights, weights), |
| 47 | ([1] * 10, [1] * 10), |
| 48 | pytest.param(["a"], ["a"], marks=pytest.mark.xfail), # cannot accept strings |
| 49 | ] |
| 50 | |
| 51 | @pytest.mark.parametrize("function_under_test,test_input,expected", encode_testdata) |
| 52 | def test_mlw_codec(self, function_under_test, test_input, expected): |
| 53 | self._call_mlw_codec_method(function_under_test, test_input, expected) |
| 54 | |
| 55 | @pytest.mark.parametrize("function_under_test,test_input,expected", decode_testdata) |
| 56 | def test_mlw_decode(self, function_under_test, test_input, expected): |
| 57 | self._call_mlw_codec_method(function_under_test, test_input, expected) |
| 58 | |
| 59 | @pytest.mark.parametrize("test_input,expected", codec_testdata) |
| 60 | def test_mlw_encode_decode(self, test_input, expected): |
| 61 | output = mlw_codec.decode(mlw_codec.encode(test_input)) |
| 62 | assert output == expected |
| 63 | |
| 64 | def _call_mlw_codec_method(self, method_name, test_input, expected): |
| 65 | output = method_name(test_input) |
| 66 | assert output == expected |
Louis Verhaard | 6023214 | 2021-01-22 14:11:15 +0100 | [diff] [blame] | 67 | |
| 68 | invalid_encode_test_data = [None, 3, [4, 5, None, 7], [0, 1, "two", 3], [1, 2, 256, 4], [2, 4, 8, -256]] |
| 69 | |
| 70 | @pytest.mark.parametrize("input", invalid_encode_test_data) |
| 71 | def test_encode_invalid_input(self, input): |
| 72 | with pytest.raises(Exception): |
| 73 | mlw_codec.encode(input) |
| 74 | |
Jonas Ohlsson | 845e232 | 2022-03-01 12:39:55 +0100 | [diff] [blame] | 75 | invalid_decode_test_data: List[Any] = [None, 3, []] |
Louis Verhaard | 6023214 | 2021-01-22 14:11:15 +0100 | [diff] [blame] | 76 | |
| 77 | @pytest.mark.parametrize("input", invalid_decode_test_data) |
| 78 | def test_decode_invalid_input(self, input): |
| 79 | with pytest.raises(Exception): |
| 80 | mlw_codec.decode(input) |