takashi kadono
/
Nucleo_446
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/TESTS/mbed_drivers/crc/main.cpp@0:8fdf9a60065b, 2018-10-10 (annotated)
- Committer:
- kadonotakashi
- Date:
- Wed Oct 10 00:33:53 2018 +0000
- Revision:
- 0:8fdf9a60065b
how to make mbed librry
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | |
kadonotakashi | 0:8fdf9a60065b | 2 | /* mbed Microcontroller Library |
kadonotakashi | 0:8fdf9a60065b | 3 | * Copyright (c) 2018 ARM Limited |
kadonotakashi | 0:8fdf9a60065b | 4 | * |
kadonotakashi | 0:8fdf9a60065b | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kadonotakashi | 0:8fdf9a60065b | 6 | * you may not use this file except in compliance with the License. |
kadonotakashi | 0:8fdf9a60065b | 7 | * You may obtain a copy of the License at |
kadonotakashi | 0:8fdf9a60065b | 8 | * |
kadonotakashi | 0:8fdf9a60065b | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
kadonotakashi | 0:8fdf9a60065b | 10 | * |
kadonotakashi | 0:8fdf9a60065b | 11 | * Unless required by applicable law or agreed to in writing, software |
kadonotakashi | 0:8fdf9a60065b | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
kadonotakashi | 0:8fdf9a60065b | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kadonotakashi | 0:8fdf9a60065b | 14 | * See the License for the specific language governing permissions and |
kadonotakashi | 0:8fdf9a60065b | 15 | * limitations under the License. |
kadonotakashi | 0:8fdf9a60065b | 16 | */ |
kadonotakashi | 0:8fdf9a60065b | 17 | |
kadonotakashi | 0:8fdf9a60065b | 18 | #include "utest/utest.h" |
kadonotakashi | 0:8fdf9a60065b | 19 | #include "unity/unity.h" |
kadonotakashi | 0:8fdf9a60065b | 20 | #include "greentea-client/test_env.h" |
kadonotakashi | 0:8fdf9a60065b | 21 | |
kadonotakashi | 0:8fdf9a60065b | 22 | #include "mbed.h" |
kadonotakashi | 0:8fdf9a60065b | 23 | |
kadonotakashi | 0:8fdf9a60065b | 24 | using namespace utest::v1; |
kadonotakashi | 0:8fdf9a60065b | 25 | |
kadonotakashi | 0:8fdf9a60065b | 26 | void test_supported_polynomials() |
kadonotakashi | 0:8fdf9a60065b | 27 | { |
kadonotakashi | 0:8fdf9a60065b | 28 | char test[] = "123456789"; |
kadonotakashi | 0:8fdf9a60065b | 29 | uint32_t crc; |
kadonotakashi | 0:8fdf9a60065b | 30 | |
kadonotakashi | 0:8fdf9a60065b | 31 | { |
kadonotakashi | 0:8fdf9a60065b | 32 | MbedCRC<POLY_7BIT_SD, 7> ct; |
kadonotakashi | 0:8fdf9a60065b | 33 | TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char *)test), &crc)); |
kadonotakashi | 0:8fdf9a60065b | 34 | TEST_ASSERT_EQUAL(0xEA, crc); |
kadonotakashi | 0:8fdf9a60065b | 35 | } |
kadonotakashi | 0:8fdf9a60065b | 36 | { |
kadonotakashi | 0:8fdf9a60065b | 37 | MbedCRC<POLY_8BIT_CCITT, 8> ct; |
kadonotakashi | 0:8fdf9a60065b | 38 | TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char *)test), &crc)); |
kadonotakashi | 0:8fdf9a60065b | 39 | TEST_ASSERT_EQUAL(0xF4, crc); |
kadonotakashi | 0:8fdf9a60065b | 40 | } |
kadonotakashi | 0:8fdf9a60065b | 41 | { |
kadonotakashi | 0:8fdf9a60065b | 42 | MbedCRC<POLY_16BIT_CCITT, 16> ct; |
kadonotakashi | 0:8fdf9a60065b | 43 | TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char *)test), &crc)); |
kadonotakashi | 0:8fdf9a60065b | 44 | TEST_ASSERT_EQUAL(0x29B1, crc); |
kadonotakashi | 0:8fdf9a60065b | 45 | } |
kadonotakashi | 0:8fdf9a60065b | 46 | { |
kadonotakashi | 0:8fdf9a60065b | 47 | MbedCRC<POLY_16BIT_IBM, 16> ct; |
kadonotakashi | 0:8fdf9a60065b | 48 | TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char *)test), &crc)); |
kadonotakashi | 0:8fdf9a60065b | 49 | TEST_ASSERT_EQUAL(0xBB3D, crc); |
kadonotakashi | 0:8fdf9a60065b | 50 | } |
kadonotakashi | 0:8fdf9a60065b | 51 | { |
kadonotakashi | 0:8fdf9a60065b | 52 | MbedCRC<POLY_32BIT_ANSI, 32> ct; |
kadonotakashi | 0:8fdf9a60065b | 53 | TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char *)test), &crc)); |
kadonotakashi | 0:8fdf9a60065b | 54 | TEST_ASSERT_EQUAL(0xCBF43926, crc); |
kadonotakashi | 0:8fdf9a60065b | 55 | } |
kadonotakashi | 0:8fdf9a60065b | 56 | } |
kadonotakashi | 0:8fdf9a60065b | 57 | |
kadonotakashi | 0:8fdf9a60065b | 58 | void test_partial_crc() |
kadonotakashi | 0:8fdf9a60065b | 59 | { |
kadonotakashi | 0:8fdf9a60065b | 60 | char test[] = "123456789"; |
kadonotakashi | 0:8fdf9a60065b | 61 | uint32_t crc; |
kadonotakashi | 0:8fdf9a60065b | 62 | { |
kadonotakashi | 0:8fdf9a60065b | 63 | MbedCRC<POLY_16BIT_CCITT, 16> ct; |
kadonotakashi | 0:8fdf9a60065b | 64 | TEST_ASSERT_EQUAL(0, ct.compute_partial_start(&crc)); |
kadonotakashi | 0:8fdf9a60065b | 65 | TEST_ASSERT_EQUAL(0, ct.compute_partial((void *)&test, 4, &crc)); |
kadonotakashi | 0:8fdf9a60065b | 66 | TEST_ASSERT_EQUAL(0, ct.compute_partial((void *)&test[4], 5, &crc)); |
kadonotakashi | 0:8fdf9a60065b | 67 | TEST_ASSERT_EQUAL(0, ct.compute_partial_stop(&crc)); |
kadonotakashi | 0:8fdf9a60065b | 68 | |
kadonotakashi | 0:8fdf9a60065b | 69 | TEST_ASSERT_EQUAL(0x29B1, crc); |
kadonotakashi | 0:8fdf9a60065b | 70 | } |
kadonotakashi | 0:8fdf9a60065b | 71 | } |
kadonotakashi | 0:8fdf9a60065b | 72 | |
kadonotakashi | 0:8fdf9a60065b | 73 | void test_sd_crc() |
kadonotakashi | 0:8fdf9a60065b | 74 | { |
kadonotakashi | 0:8fdf9a60065b | 75 | MbedCRC<POLY_7BIT_SD, 7> crc7; |
kadonotakashi | 0:8fdf9a60065b | 76 | uint32_t crc; |
kadonotakashi | 0:8fdf9a60065b | 77 | char test[512]; |
kadonotakashi | 0:8fdf9a60065b | 78 | |
kadonotakashi | 0:8fdf9a60065b | 79 | test[0] = 0x40; |
kadonotakashi | 0:8fdf9a60065b | 80 | test[1] = 0x00; |
kadonotakashi | 0:8fdf9a60065b | 81 | test[2] = 0x00; |
kadonotakashi | 0:8fdf9a60065b | 82 | test[3] = 0x00; |
kadonotakashi | 0:8fdf9a60065b | 83 | test[4] = 0x00; |
kadonotakashi | 0:8fdf9a60065b | 84 | TEST_ASSERT_EQUAL(0, crc7.compute((void *)test, 5, &crc)); |
kadonotakashi | 0:8fdf9a60065b | 85 | crc = (crc | 0x1) & 0xFF; |
kadonotakashi | 0:8fdf9a60065b | 86 | TEST_ASSERT_EQUAL(0x95, crc); |
kadonotakashi | 0:8fdf9a60065b | 87 | |
kadonotakashi | 0:8fdf9a60065b | 88 | test[0] = 0x48; |
kadonotakashi | 0:8fdf9a60065b | 89 | test[1] = 0x00; |
kadonotakashi | 0:8fdf9a60065b | 90 | test[2] = 0x00; |
kadonotakashi | 0:8fdf9a60065b | 91 | test[3] = 0x01; |
kadonotakashi | 0:8fdf9a60065b | 92 | test[4] = 0xAA; |
kadonotakashi | 0:8fdf9a60065b | 93 | TEST_ASSERT_EQUAL(0, crc7.compute((void *)test, 5, &crc)); |
kadonotakashi | 0:8fdf9a60065b | 94 | crc = (crc | 0x1) & 0xFF; |
kadonotakashi | 0:8fdf9a60065b | 95 | TEST_ASSERT_EQUAL(0x87, crc); |
kadonotakashi | 0:8fdf9a60065b | 96 | |
kadonotakashi | 0:8fdf9a60065b | 97 | test[0] = 0x51; |
kadonotakashi | 0:8fdf9a60065b | 98 | test[1] = 0x00; |
kadonotakashi | 0:8fdf9a60065b | 99 | test[2] = 0x00; |
kadonotakashi | 0:8fdf9a60065b | 100 | test[3] = 0x00; |
kadonotakashi | 0:8fdf9a60065b | 101 | test[4] = 0x00; |
kadonotakashi | 0:8fdf9a60065b | 102 | TEST_ASSERT_EQUAL(0, crc7.compute((void *)test, 5, &crc)); |
kadonotakashi | 0:8fdf9a60065b | 103 | crc = (crc | 0x1) & 0xFF; |
kadonotakashi | 0:8fdf9a60065b | 104 | TEST_ASSERT_EQUAL(0x55, crc); |
kadonotakashi | 0:8fdf9a60065b | 105 | |
kadonotakashi | 0:8fdf9a60065b | 106 | MbedCRC<POLY_16BIT_CCITT, 16> crc16(0, 0, false, false); |
kadonotakashi | 0:8fdf9a60065b | 107 | memset(test, 0xFF, 512); |
kadonotakashi | 0:8fdf9a60065b | 108 | TEST_ASSERT_EQUAL(0, crc16.compute((void *)test, 512, &crc)); |
kadonotakashi | 0:8fdf9a60065b | 109 | TEST_ASSERT_EQUAL(0x7FA1, crc); |
kadonotakashi | 0:8fdf9a60065b | 110 | } |
kadonotakashi | 0:8fdf9a60065b | 111 | |
kadonotakashi | 0:8fdf9a60065b | 112 | void test_any_polynomial() |
kadonotakashi | 0:8fdf9a60065b | 113 | { |
kadonotakashi | 0:8fdf9a60065b | 114 | char test[] = "123456789"; |
kadonotakashi | 0:8fdf9a60065b | 115 | uint32_t crc; |
kadonotakashi | 0:8fdf9a60065b | 116 | { |
kadonotakashi | 0:8fdf9a60065b | 117 | MbedCRC<0x3D65, 16> ct(0x0, 0xFFFF, 0, 0); |
kadonotakashi | 0:8fdf9a60065b | 118 | TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char *)test), &crc)); |
kadonotakashi | 0:8fdf9a60065b | 119 | TEST_ASSERT_EQUAL(0xC2B7, crc); |
kadonotakashi | 0:8fdf9a60065b | 120 | } |
kadonotakashi | 0:8fdf9a60065b | 121 | { |
kadonotakashi | 0:8fdf9a60065b | 122 | MbedCRC<0x1EDC6F41, 32> ct(0xFFFFFFFF, 0xFFFFFFFF, 1, 1); |
kadonotakashi | 0:8fdf9a60065b | 123 | TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char *)test), &crc)); |
kadonotakashi | 0:8fdf9a60065b | 124 | TEST_ASSERT_EQUAL(0xE3069283, crc); |
kadonotakashi | 0:8fdf9a60065b | 125 | } |
kadonotakashi | 0:8fdf9a60065b | 126 | } |
kadonotakashi | 0:8fdf9a60065b | 127 | |
kadonotakashi | 0:8fdf9a60065b | 128 | void test_thread(void) |
kadonotakashi | 0:8fdf9a60065b | 129 | { |
kadonotakashi | 0:8fdf9a60065b | 130 | char test[] = "123456789"; |
kadonotakashi | 0:8fdf9a60065b | 131 | uint32_t crc; |
kadonotakashi | 0:8fdf9a60065b | 132 | MbedCRC<POLY_32BIT_ANSI, 32> ct; |
kadonotakashi | 0:8fdf9a60065b | 133 | TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char*)test), &crc)); |
kadonotakashi | 0:8fdf9a60065b | 134 | TEST_ASSERT_EQUAL(0xCBF43926, crc); |
kadonotakashi | 0:8fdf9a60065b | 135 | } |
kadonotakashi | 0:8fdf9a60065b | 136 | |
kadonotakashi | 0:8fdf9a60065b | 137 | void test_thread_safety() |
kadonotakashi | 0:8fdf9a60065b | 138 | { |
kadonotakashi | 0:8fdf9a60065b | 139 | char test[] = "123456789"; |
kadonotakashi | 0:8fdf9a60065b | 140 | uint32_t crc; |
kadonotakashi | 0:8fdf9a60065b | 141 | |
kadonotakashi | 0:8fdf9a60065b | 142 | MbedCRC<POLY_16BIT_IBM, 16> ct; |
kadonotakashi | 0:8fdf9a60065b | 143 | |
kadonotakashi | 0:8fdf9a60065b | 144 | TEST_ASSERT_EQUAL(0, ct.compute_partial_start(&crc)); |
kadonotakashi | 0:8fdf9a60065b | 145 | TEST_ASSERT_EQUAL(0, ct.compute_partial((void *)&test, 4, &crc)); |
kadonotakashi | 0:8fdf9a60065b | 146 | |
kadonotakashi | 0:8fdf9a60065b | 147 | Thread t1(osPriorityNormal1, 320); |
kadonotakashi | 0:8fdf9a60065b | 148 | t1.start(callback(test_thread)); |
kadonotakashi | 0:8fdf9a60065b | 149 | TEST_ASSERT_EQUAL(0, ct.compute_partial((void *)&test[4], 5, &crc)); |
kadonotakashi | 0:8fdf9a60065b | 150 | TEST_ASSERT_EQUAL(0, ct.compute_partial_stop(&crc)); |
kadonotakashi | 0:8fdf9a60065b | 151 | TEST_ASSERT_EQUAL(0xBB3D, crc); |
kadonotakashi | 0:8fdf9a60065b | 152 | |
kadonotakashi | 0:8fdf9a60065b | 153 | // Wait for the thread to finish |
kadonotakashi | 0:8fdf9a60065b | 154 | t1.join(); |
kadonotakashi | 0:8fdf9a60065b | 155 | } |
kadonotakashi | 0:8fdf9a60065b | 156 | |
kadonotakashi | 0:8fdf9a60065b | 157 | Case cases[] = { |
kadonotakashi | 0:8fdf9a60065b | 158 | Case("Test supported polynomials", test_supported_polynomials), |
kadonotakashi | 0:8fdf9a60065b | 159 | Case("Test partial CRC", test_partial_crc), |
kadonotakashi | 0:8fdf9a60065b | 160 | Case("Test SD CRC polynomials", test_sd_crc), |
kadonotakashi | 0:8fdf9a60065b | 161 | Case("Test not supported polynomials", test_any_polynomial), |
kadonotakashi | 0:8fdf9a60065b | 162 | Case("Test thread safety", test_thread_safety) |
kadonotakashi | 0:8fdf9a60065b | 163 | }; |
kadonotakashi | 0:8fdf9a60065b | 164 | |
kadonotakashi | 0:8fdf9a60065b | 165 | utest::v1::status_t greentea_test_setup(const size_t number_of_cases) |
kadonotakashi | 0:8fdf9a60065b | 166 | { |
kadonotakashi | 0:8fdf9a60065b | 167 | GREENTEA_SETUP(15, "default_auto"); |
kadonotakashi | 0:8fdf9a60065b | 168 | return greentea_test_setup_handler(number_of_cases); |
kadonotakashi | 0:8fdf9a60065b | 169 | } |
kadonotakashi | 0:8fdf9a60065b | 170 | |
kadonotakashi | 0:8fdf9a60065b | 171 | Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
kadonotakashi | 0:8fdf9a60065b | 172 | |
kadonotakashi | 0:8fdf9a60065b | 173 | int main() |
kadonotakashi | 0:8fdf9a60065b | 174 | { |
kadonotakashi | 0:8fdf9a60065b | 175 | Harness::run(specification); |
kadonotakashi | 0:8fdf9a60065b | 176 | } |