|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2018 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include "mbed.h" |
| 17 | +#include "greentea-client/test_env.h" |
| 18 | +#include "unity.h" |
| 19 | +#include "utest.h" |
| 20 | + |
| 21 | +#include "FlashSimBlockDevice.h" |
| 22 | +#include "HeapBlockDevice.h" |
| 23 | +#include <stdlib.h> |
| 24 | + |
| 25 | +using namespace utest::v1; |
| 26 | + |
| 27 | +static const bd_size_t read_size = 1; |
| 28 | +static const bd_size_t prog_size = 8; |
| 29 | +static const bd_size_t erase_size = 512; |
| 30 | +static const bd_size_t num_blocks = 4; |
| 31 | +static const bd_size_t test_buf_size = 64; |
| 32 | +static const uint8_t blank = 0xFF; |
| 33 | + |
| 34 | +// Simple test for all APIs |
| 35 | +void functionality_test() |
| 36 | +{ |
| 37 | + HeapBlockDevice heap_bd(num_blocks * erase_size, read_size, prog_size, erase_size); |
| 38 | + FlashSimBlockDevice bd(&heap_bd, blank); |
| 39 | + |
| 40 | + int err = bd.init(); |
| 41 | + TEST_ASSERT_EQUAL(0, err); |
| 42 | + |
| 43 | + uint8_t read_buf[test_buf_size], write_buf[test_buf_size]; |
| 44 | + |
| 45 | + TEST_ASSERT_EQUAL(num_blocks * erase_size, bd.size()); |
| 46 | + TEST_ASSERT_EQUAL(read_size, bd.get_read_size()); |
| 47 | + TEST_ASSERT_EQUAL(prog_size, bd.get_program_size()); |
| 48 | + TEST_ASSERT_EQUAL(erase_size, bd.get_erase_size()); |
| 49 | + TEST_ASSERT_EQUAL(blank, bd.get_erase_value()); |
| 50 | + |
| 51 | + srand(1); |
| 52 | + for (bd_size_t i = 0; i < test_buf_size; i++) { |
| 53 | + write_buf[i] = 0xff & rand(); |
| 54 | + } |
| 55 | + |
| 56 | + // Make sure we can't program if not erased (even after init) |
| 57 | + err = bd.program(write_buf, 0, test_buf_size); |
| 58 | + TEST_ASSERT_EQUAL(BD_ERROR_NOT_ERASED, err); |
| 59 | + |
| 60 | + err = bd.erase(0, erase_size * 2); |
| 61 | + TEST_ASSERT_EQUAL(0, err); |
| 62 | + |
| 63 | + err = bd.program(write_buf, 0, test_buf_size); |
| 64 | + TEST_ASSERT_EQUAL(0, err); |
| 65 | + |
| 66 | + // Allow programming same data |
| 67 | + err = bd.program(write_buf, 0, test_buf_size); |
| 68 | + TEST_ASSERT_EQUAL(0, err); |
| 69 | + |
| 70 | + srand(2); |
| 71 | + for (bd_size_t i = 0; i < test_buf_size; i++) { |
| 72 | + write_buf[i] = 0xff & rand(); |
| 73 | + } |
| 74 | + |
| 75 | + err = bd.program(write_buf, 0, test_buf_size); |
| 76 | + TEST_ASSERT_EQUAL(BD_ERROR_NOT_ERASED, err); |
| 77 | + |
| 78 | + err = bd.program(write_buf, 2 * erase_size - test_buf_size, test_buf_size); |
| 79 | + TEST_ASSERT_EQUAL(0, err); |
| 80 | + |
| 81 | + memset(write_buf, blank, test_buf_size / 2); |
| 82 | + err = bd.read(read_buf, test_buf_size * 2, test_buf_size / 2); |
| 83 | + TEST_ASSERT_EQUAL(0, err); |
| 84 | + TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, test_buf_size / 2); |
| 85 | + |
| 86 | + srand(1); |
| 87 | + for (bd_size_t i = 0; i < test_buf_size; i++) { |
| 88 | + write_buf[i] = 0xff & rand(); |
| 89 | + } |
| 90 | + |
| 91 | + err = bd.read(read_buf, 0, test_buf_size); |
| 92 | + TEST_ASSERT_EQUAL(0, err); |
| 93 | + TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, test_buf_size); |
| 94 | + |
| 95 | + srand(2); |
| 96 | + for (bd_size_t i = 0; i < test_buf_size; i++) { |
| 97 | + write_buf[i] = 0xff & rand(); |
| 98 | + } |
| 99 | + |
| 100 | + err = bd.read(read_buf, 2 * erase_size - test_buf_size, test_buf_size); |
| 101 | + TEST_ASSERT_EQUAL(0, err); |
| 102 | + TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, test_buf_size); |
| 103 | + |
| 104 | + err = bd.deinit(); |
| 105 | + TEST_ASSERT_EQUAL(0, err); |
| 106 | + |
| 107 | + err = bd.init(); |
| 108 | + TEST_ASSERT_EQUAL(0, err); |
| 109 | + |
| 110 | + // Make sure data lives across inits |
| 111 | + err = bd.read(read_buf, 2 * erase_size - test_buf_size, test_buf_size); |
| 112 | + TEST_ASSERT_EQUAL(0, err); |
| 113 | + TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, test_buf_size); |
| 114 | + |
| 115 | + err = bd.erase(0, erase_size); |
| 116 | + TEST_ASSERT_EQUAL(0, err); |
| 117 | + |
| 118 | + // Make sure erase returns the erase value |
| 119 | + memset(write_buf, blank, test_buf_size); |
| 120 | + err = bd.read(read_buf, 0, test_buf_size); |
| 121 | + TEST_ASSERT_EQUAL(0, err); |
| 122 | + TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, test_buf_size); |
| 123 | +} |
| 124 | + |
| 125 | + |
| 126 | +// Test setup |
| 127 | +utest::v1::status_t test_setup(const size_t number_of_cases) |
| 128 | +{ |
| 129 | + GREENTEA_SETUP(30, "default_auto"); |
| 130 | + return verbose_test_setup_handler(number_of_cases); |
| 131 | +} |
| 132 | + |
| 133 | +Case cases[] = { |
| 134 | + Case("FlashSimBlockDevice functionality test", functionality_test), |
| 135 | +}; |
| 136 | + |
| 137 | +Specification specification(test_setup, cases); |
| 138 | + |
| 139 | +int main() |
| 140 | +{ |
| 141 | + return !Harness::run(specification); |
| 142 | +} |
0 commit comments