aboutsummaryrefslogtreecommitdiff
path: root/pw_rpc/py/tests/ids_test.py
blob: fed5b787d664fb542e50f4749ab2560fd0725f39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
# Copyright 2020 The Pigweed Authors
#
# 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
#
#     https://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.
"""Tests service and method ID calculation for Python and C++."""

from typing import Iterator
import unittest

from pw_build.generated_tests import Context, TestGenerator
from pw_build import generated_tests
from pw_rpc import ids

_TESTS = TestGenerator(
    [
        'Empty string',
        (0x00000000, ''),
        'Single character strings',
        (0x00000001, '\0'),
        (0x00010040, '\1'),
        (0x003F0F82, '?'),
        'Non-printable strings',
        (0xD3556087, '\0\0\0\1\1\1\1'),
        'General strings',
        (0x63D43D8C, 'Pigweed?'),
        (0x79AB6494, 'Pigweed!Pigweed!Pigweed!Pigweed!Pigweed!Pigweed!'),
    ]
)


def _define_py_test(ctx: Context):
    expected_id, name = ctx.test_case
    return lambda self: self.assertEqual(expected_id, ids.calculate(name))


IdsTest = _TESTS.python_tests('IdsTest', _define_py_test)

_CC_HEADER = """\
#include <string_view>

#include "gtest/gtest.h"
#include "pw_rpc/internal/hash.h"

namespace pw::rpc::internal {

using namespace std::string_view_literals;
"""

_CC_FOOTER = '}  // namespace pw::rpc::internal'


def _cc_test(ctx: Context) -> Iterator[str]:
    expected_id, name = ctx.test_case

    yield f'TEST(RpcIds, {ctx.cc_name()}) {{'
    yield f'    EXPECT_EQ(0x{expected_id:08x}u,'
    yield f'              Hash({generated_tests.cc_string(name)}sv));'
    yield '}'


if __name__ == '__main__':
    args = generated_tests.parse_test_generation_args()
    if args.generate_cc_test:
        _TESTS.cc_tests(args.generate_cc_test, _cc_test, _CC_HEADER, _CC_FOOTER)
    else:
        unittest.main()