|
| 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 "greentea-client/test_env.h" |
| 17 | +#include "unity.h" |
| 18 | +#include "utest.h" |
| 19 | + |
| 20 | +#include "BufferedBlockDevice.h" |
| 21 | +#include "HeapBlockDevice.h" |
| 22 | +#include <stdlib.h> |
| 23 | + |
| 24 | +using namespace utest::v1; |
| 25 | + |
| 26 | +static const bd_size_t heap_erase_size = 512; |
| 27 | +static const bd_size_t heap_prog_size = heap_erase_size; |
| 28 | +static const bd_size_t heap_read_size = 256; |
| 29 | +static const bd_size_t num_blocks = 4; |
| 30 | + |
| 31 | +void functionality_test() |
| 32 | +{ |
| 33 | + HeapBlockDevice heap_bd(num_blocks * heap_erase_size, heap_read_size, heap_prog_size, heap_erase_size); |
| 34 | + BufferedBlockDevice bd(&heap_bd); |
| 35 | + |
| 36 | + int err = bd.init(); |
| 37 | + TEST_ASSERT_EQUAL(0, err); |
| 38 | + |
| 39 | + uint8_t *read_buf, *write_buf; |
| 40 | + read_buf = new uint8_t[heap_prog_size]; |
| 41 | + write_buf = new uint8_t[heap_prog_size]; |
| 42 | + |
| 43 | + TEST_ASSERT_EQUAL(1, bd.get_read_size()); |
| 44 | + TEST_ASSERT_EQUAL(1, bd.get_program_size()); |
| 45 | + TEST_ASSERT_EQUAL(heap_erase_size, bd.get_erase_size()); |
| 46 | + |
| 47 | + for (bd_size_t i = 0; i < num_blocks; i++) { |
| 48 | + memset(write_buf, i, heap_prog_size); |
| 49 | + err = heap_bd.program(write_buf, i * heap_prog_size, heap_prog_size); |
| 50 | + TEST_ASSERT_EQUAL(0, err); |
| 51 | + } |
| 52 | + |
| 53 | + err = bd.read(read_buf, heap_prog_size + heap_prog_size / 2, 1); |
| 54 | + TEST_ASSERT_EQUAL(0, err); |
| 55 | + TEST_ASSERT_EQUAL(1, read_buf[0]); |
| 56 | + |
| 57 | + err = bd.read(read_buf, 2 * heap_prog_size + heap_prog_size / 2, 4); |
| 58 | + TEST_ASSERT_EQUAL(0, err); |
| 59 | + memset(write_buf, 2, 4); |
| 60 | + TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, 4); |
| 61 | + |
| 62 | + memset(write_buf, 1, heap_prog_size); |
| 63 | + memset(write_buf + 64, 0x5A, 8); |
| 64 | + memset(write_buf + 72, 0xA5, 8); |
| 65 | + err = bd.program(write_buf + 64, heap_prog_size + 64, 8); |
| 66 | + TEST_ASSERT_EQUAL(0, err); |
| 67 | + err = bd.program(write_buf + 72, heap_prog_size + 72, 8); |
| 68 | + TEST_ASSERT_EQUAL(0, err); |
| 69 | + err = bd.read(read_buf, heap_prog_size, heap_prog_size); |
| 70 | + TEST_ASSERT_EQUAL(0, err); |
| 71 | + TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, heap_prog_size); |
| 72 | + memset(write_buf, 1, heap_prog_size); |
| 73 | + // Underlying BD should not be updated before sync |
| 74 | + err = heap_bd.read(read_buf, heap_prog_size, heap_prog_size); |
| 75 | + TEST_ASSERT_EQUAL(0, err); |
| 76 | + TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, heap_prog_size); |
| 77 | + err = bd.sync(); |
| 78 | + TEST_ASSERT_EQUAL(0, err); |
| 79 | + memset(write_buf + 64, 0x5A, 8); |
| 80 | + memset(write_buf + 72, 0xA5, 8); |
| 81 | + // Should be updated now |
| 82 | + err = bd.read(read_buf, heap_prog_size, heap_prog_size); |
| 83 | + TEST_ASSERT_EQUAL(0, err); |
| 84 | + TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, heap_prog_size); |
| 85 | + err = heap_bd.read(read_buf, heap_prog_size, heap_prog_size); |
| 86 | + TEST_ASSERT_EQUAL(0, err); |
| 87 | + TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, heap_prog_size); |
| 88 | + |
| 89 | + memset(write_buf, 0xAA, 16); |
| 90 | + memset(write_buf + 16, 0xBB, 16); |
| 91 | + err = bd.program(write_buf, 3 * heap_prog_size - 16, 32); |
| 92 | + TEST_ASSERT_EQUAL(0, err); |
| 93 | + // First block should sync, but second still shouldn't |
| 94 | + memset(write_buf, 2, heap_prog_size - 16); |
| 95 | + memset(write_buf + heap_prog_size - 16, 0xAA, 16); |
| 96 | + err = heap_bd.read(read_buf, 2 * heap_prog_size, heap_prog_size); |
| 97 | + TEST_ASSERT_EQUAL(0, err); |
| 98 | + TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, heap_prog_size); |
| 99 | + memset(write_buf, 3, heap_prog_size); |
| 100 | + err = heap_bd.read(read_buf, 3 * heap_prog_size, heap_prog_size); |
| 101 | + TEST_ASSERT_EQUAL(0, err); |
| 102 | + TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, heap_prog_size); |
| 103 | + memset(write_buf, 0xBB, 16); |
| 104 | + err = bd.read(read_buf, 3 * heap_prog_size, heap_prog_size); |
| 105 | + TEST_ASSERT_EQUAL(0, err); |
| 106 | + TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, heap_prog_size); |
| 107 | + // Moving to another block should automatically sync |
| 108 | + err = bd.read(read_buf, 15, 1); |
| 109 | + TEST_ASSERT_EQUAL(0, err); |
| 110 | + TEST_ASSERT_EQUAL(0, read_buf[0]); |
| 111 | + err = heap_bd.read(read_buf, 3 * heap_prog_size, heap_prog_size); |
| 112 | + TEST_ASSERT_EQUAL(0, err); |
| 113 | + TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, heap_prog_size); |
| 114 | + err = bd.read(read_buf, 3 * heap_prog_size, heap_prog_size); |
| 115 | + TEST_ASSERT_EQUAL(0, err); |
| 116 | + TEST_ASSERT_EQUAL_UINT8_ARRAY(write_buf, read_buf, heap_prog_size); |
| 117 | + |
| 118 | + delete[] read_buf; |
| 119 | + delete[] write_buf; |
| 120 | +} |
| 121 | + |
| 122 | +// Test setup |
| 123 | +utest::v1::status_t test_setup(const size_t number_of_cases) |
| 124 | +{ |
| 125 | + GREENTEA_SETUP(30, "default_auto"); |
| 126 | + return verbose_test_setup_handler(number_of_cases); |
| 127 | +} |
| 128 | + |
| 129 | +Case cases[] = { |
| 130 | + Case("BufferedBlockDevice functionality test", functionality_test), |
| 131 | +}; |
| 132 | + |
| 133 | +Specification specification(test_setup, cases); |
| 134 | + |
| 135 | +int main() |
| 136 | +{ |
| 137 | + return !Harness::run(specification); |
| 138 | +} |
| 139 | + |
0 commit comments