This library contains code that extends and simplifies different operations
for C language based programs.
Revisión | 358bd7cd2e0ea002127d68dcbbd4945e3e28ac7e (tree) |
---|---|
Tiempo | 2023-08-26 06:49:16 |
Autor | Sergey Gusarov <laborer2008@gmai...> |
Commiter | Sergey Gusarov |
Added alphabet_consts module
@@ -1,4 +1,4 @@ | ||
1 | 1 | a799a417ac6770fd96892416bc7148144ea600f6 cmake/cmake_tools |
2 | 2 | 5013d507802becd2c434d4dbdafad45c3da5ef23 include/ctools/std/msinttypes |
3 | -c99ed6de035765057f05a5b8a3bcd23d47256b0c pyrepo | |
3 | +a8253b9c71905e48fa60b9c39910131d985bab16 pyrepo | |
4 | 4 | 0b899aec14d3a9abb2bf260ac355f0f28630a6a3 tests/unity |
@@ -0,0 +1,52 @@ | ||
1 | +/* | |
2 | + * @author Sergey Gusarov <laborer2008 (at) gmail.com> | |
3 | + * @section LICENSE | |
4 | + * This Source Code Form is subject to the terms of the Mozilla Public | |
5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this | |
6 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
7 | + * | |
8 | + * @section DESCRIPTION | |
9 | + * Alphabet constants. They are useful to avoid using of magic constants | |
10 | + */ | |
11 | + | |
12 | +#pragma once | |
13 | + | |
14 | +#include "ctools/predef/attributes.h" | |
15 | +#include "ctools/predef/cxx11_attributes.h" | |
16 | +#include "ctools/namespace.h" | |
17 | +#include "ctools/std/stdbool.h" | |
18 | +#include "ctools/std/stdint.h" | |
19 | + | |
20 | +/* | |
21 | + * These constants depend on the current codepage so they are runtime and cannot be enum-based | |
22 | + */ | |
23 | +#define CT_RUSSIAN_CAPITAL_A 'А' | |
24 | +#define CT_RUSSIAN_CAPITAL_JA 'Я' | |
25 | + | |
26 | +#define CT_RUSSIAN_SMALL_A 'а' | |
27 | +#define CT_RUSSIAN_SMALL_JA 'я' | |
28 | + | |
29 | + | |
30 | +CT_BEGIN_NAMESPACE | |
31 | + | |
32 | +/* *INDENT-OFF* */ | |
33 | +// Base constants | |
34 | +enum {kEnglishCapitalA = 'A'}; | |
35 | +enum {kEnglishCapitalZ = 'Z'}; | |
36 | + | |
37 | +enum {kEnglishSmallA = 'a'}; | |
38 | +enum {kEnglishSmallZ = 'z'}; | |
39 | + | |
40 | +/* | |
41 | + * Pre-condition: v is alphabetic ASCII, upper or lower case | |
42 | + * Borrowe from: https://stackoverflow.com/a/47846874 | |
43 | + */ | |
44 | +static CT_FORCEINLINE bool isVowel(const char v) CT_NOEXCEPT | |
45 | +{ | |
46 | + // 0x208222 without 'Y' and 0x2208222 with 'Y' | |
47 | + return (0x2208222 >> (v & 0x1f)) & 1; | |
48 | +} | |
49 | + | |
50 | +/* *INDENT-ON* */ | |
51 | + | |
52 | +CT_END_NAMESPACE |
@@ -0,0 +1,64 @@ | ||
1 | +/* | |
2 | + * @author Sergey Gusarov <laborer2008 (at) gmail.com> | |
3 | + * @section LICENSE | |
4 | + * This Source Code Form is subject to the terms of the Mozilla Public | |
5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this | |
6 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
7 | + * | |
8 | + * @section DESCRIPTION | |
9 | + * | |
10 | + */ | |
11 | + | |
12 | +#include "tests/unity_headers.h" | |
13 | + | |
14 | +#include <ctools/alphabet_consts.h> | |
15 | +#include <ctools/unused.h> | |
16 | + | |
17 | + | |
18 | +void setUp(void) | |
19 | +{ | |
20 | +} | |
21 | + | |
22 | +void tearDown(void) | |
23 | +{ | |
24 | +} | |
25 | + | |
26 | +void testConstants(void) | |
27 | +{ | |
28 | + int a; | |
29 | + | |
30 | + // TODO: test russian constants | |
31 | + | |
32 | + a = kEnglishCapitalA; | |
33 | + CT_UNUSED(a); | |
34 | + | |
35 | + a = kEnglishCapitalZ; | |
36 | + CT_UNUSED(a); | |
37 | + | |
38 | + a = kEnglishSmallA; | |
39 | + CT_UNUSED(a); | |
40 | + | |
41 | + a = kEnglishSmallZ; | |
42 | + CT_UNUSED(a); | |
43 | +} | |
44 | + | |
45 | +void testCompile(void) | |
46 | +{ | |
47 | + TEST_ASSERT_TRUE(isVowel('A')); | |
48 | + TEST_ASSERT_TRUE(isVowel('E')); | |
49 | + TEST_ASSERT_TRUE(isVowel('I')); | |
50 | + TEST_ASSERT_TRUE(isVowel('O')); | |
51 | + TEST_ASSERT_TRUE(isVowel('U')); | |
52 | + TEST_ASSERT_TRUE(isVowel('Y')); | |
53 | + TEST_ASSERT_FALSE(isVowel('B')); | |
54 | + TEST_ASSERT_FALSE(isVowel('Z')); | |
55 | + | |
56 | + TEST_ASSERT_TRUE(isVowel('a')); | |
57 | + TEST_ASSERT_TRUE(isVowel('e')); | |
58 | + TEST_ASSERT_TRUE(isVowel('i')); | |
59 | + TEST_ASSERT_TRUE(isVowel('o')); | |
60 | + TEST_ASSERT_TRUE(isVowel('u')); | |
61 | + TEST_ASSERT_TRUE(isVowel('y')); | |
62 | + TEST_ASSERT_FALSE(isVowel('c')); | |
63 | + TEST_ASSERT_FALSE(isVowel('z')); | |
64 | +} |