|
| 1 | + |
| 2 | +/* mbed Microcontroller Library |
| 3 | + * Copyright (c) 2018 ARM Limited |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "utest/utest.h" |
| 19 | +#include "unity/unity.h" |
| 20 | +#include "greentea-client/test_env.h" |
| 21 | + |
| 22 | +#include "mbed.h" |
| 23 | + |
| 24 | +using namespace utest::v1; |
| 25 | + |
| 26 | +void test_supported_polynomials() |
| 27 | +{ |
| 28 | + char test[] = "123456789"; |
| 29 | + uint32_t crc; |
| 30 | + |
| 31 | + { |
| 32 | + MbedCRC<POLY_7BIT_SD, 7> ct; |
| 33 | + TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char*)test), &crc)); |
| 34 | + TEST_ASSERT_EQUAL(0xEA, crc); |
| 35 | + } |
| 36 | + { |
| 37 | + MbedCRC<POLY_8BIT_CCITT, 8> ct; |
| 38 | + TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char*)test), &crc)); |
| 39 | + TEST_ASSERT_EQUAL(0xF4, crc); |
| 40 | + } |
| 41 | + { |
| 42 | + MbedCRC<POLY_16BIT_CCITT, 16> ct; |
| 43 | + TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char*)test), &crc)); |
| 44 | + TEST_ASSERT_EQUAL(0x29B1, crc); |
| 45 | + } |
| 46 | + { |
| 47 | + MbedCRC<POLY_16BIT_IBM, 16> ct; |
| 48 | + TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char*)test), &crc)); |
| 49 | + TEST_ASSERT_EQUAL(0xBB3D, crc); |
| 50 | + } |
| 51 | + { |
| 52 | + MbedCRC<POLY_32BIT_ANSI, 32> ct; |
| 53 | + TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char*)test), &crc)); |
| 54 | + TEST_ASSERT_EQUAL(0xCBF43926, crc); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +void test_partial_crc() |
| 59 | +{ |
| 60 | + char test[] = "123456789"; |
| 61 | + uint32_t crc; |
| 62 | + { |
| 63 | + MbedCRC<POLY_16BIT_CCITT, 16> ct; |
| 64 | + TEST_ASSERT_EQUAL(0, ct.compute_partial_start(&crc)); |
| 65 | + TEST_ASSERT_EQUAL(0, ct.compute_partial((void *)&test, 4, &crc)); |
| 66 | + TEST_ASSERT_EQUAL(0, ct.compute_partial((void *)&test[4], 5, &crc)); |
| 67 | + TEST_ASSERT_EQUAL(0, ct.compute_partial_stop(&crc)); |
| 68 | + |
| 69 | + TEST_ASSERT_EQUAL(0x29B1, crc); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +void test_sd_crc() |
| 74 | +{ |
| 75 | + MbedCRC<POLY_7BIT_SD, 7> crc7; |
| 76 | + uint32_t crc; |
| 77 | + char test[512]; |
| 78 | + |
| 79 | + test[0] = 0x40; |
| 80 | + test[1] = 0x00; |
| 81 | + test[2] = 0x00; |
| 82 | + test[3] = 0x00; |
| 83 | + test[4] = 0x00; |
| 84 | + TEST_ASSERT_EQUAL(0, crc7.compute((void *)test, 5, &crc)); |
| 85 | + crc = (crc | 0x1 ) & 0xFF; |
| 86 | + TEST_ASSERT_EQUAL(0x95, crc); |
| 87 | + |
| 88 | + test[0] = 0x48; |
| 89 | + test[1] = 0x00; |
| 90 | + test[2] = 0x00; |
| 91 | + test[3] = 0x01; |
| 92 | + test[4] = 0xAA; |
| 93 | + TEST_ASSERT_EQUAL(0, crc7.compute((void *)test, 5, &crc)); |
| 94 | + crc = (crc | 0x1 ) & 0xFF; |
| 95 | + TEST_ASSERT_EQUAL(0x87, crc); |
| 96 | + |
| 97 | + test[0] = 0x51; |
| 98 | + test[1] = 0x00; |
| 99 | + test[2] = 0x00; |
| 100 | + test[3] = 0x00; |
| 101 | + test[4] = 0x00; |
| 102 | + TEST_ASSERT_EQUAL(0, crc7.compute((void *)test, 5, &crc)); |
| 103 | + crc = (crc | 0x1 ) & 0xFF; |
| 104 | + TEST_ASSERT_EQUAL(0x55, crc); |
| 105 | + |
| 106 | + MbedCRC<POLY_16BIT_CCITT, 16> crc16(0, 0, false, false); |
| 107 | + memset(test, 0xFF, 512); |
| 108 | + TEST_ASSERT_EQUAL(0, crc16.compute((void *)test, 512, &crc)); |
| 109 | + TEST_ASSERT_EQUAL(0x7FA1, crc); |
| 110 | +} |
| 111 | + |
| 112 | +void test_any_polynomial() |
| 113 | +{ |
| 114 | + char test[] = "123456789"; |
| 115 | + uint32_t crc; |
| 116 | + { |
| 117 | + MbedCRC<0x3D65, 16> ct(0x0, 0xFFFF, 0, 0); |
| 118 | + TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char*)test), &crc)); |
| 119 | + TEST_ASSERT_EQUAL(0xC2B7, crc); |
| 120 | + } |
| 121 | + { |
| 122 | + MbedCRC<0x1EDC6F41, 32> ct(0xFFFFFFFF, 0xFFFFFFFF, 1, 1); |
| 123 | + TEST_ASSERT_EQUAL(0, ct.compute((void *)test, strlen((const char*)test), &crc)); |
| 124 | + TEST_ASSERT_EQUAL(0xE3069283, crc); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +Case cases[] = { |
| 129 | + Case("Test supported polynomials", test_supported_polynomials), |
| 130 | + Case("Test partial CRC", test_partial_crc), |
| 131 | + Case("Test SD CRC polynomials", test_sd_crc), |
| 132 | + Case("Test not supported polynomials", test_any_polynomial) |
| 133 | +}; |
| 134 | + |
| 135 | +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { |
| 136 | + GREENTEA_SETUP(15, "default_auto"); |
| 137 | + return greentea_test_setup_handler(number_of_cases); |
| 138 | +} |
| 139 | + |
| 140 | +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
| 141 | + |
| 142 | +int main() { |
| 143 | + Harness::run(specification); |
| 144 | +} |
0 commit comments