init

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "mbed.h"
00017 #include "greentea-client/test_env.h"
00018 #include "unity.h"
00019 #include "utest.h"
00020 
00021 #include "HeapBlockDevice.h"
00022 #include "SlicingBlockDevice.h"
00023 #include "ChainingBlockDevice.h"
00024 #include "ProfilingBlockDevice.h"
00025 #include <stdlib.h>
00026 
00027 using namespace utest::v1;
00028 
00029 // TODO HACK, replace with available ram/heap property
00030 #if defined(TARGET_MTB_MTS_XDOT)
00031     #error [NOT_SUPPORTED] Insufficient heap for heap block device tests
00032 #endif
00033 
00034 #define BLOCK_COUNT 16
00035 #define BLOCK_SIZE 512
00036 
00037 
00038 // Simple test which read/writes blocks on a sliced block device
00039 void test_slicing() {
00040     HeapBlockDevice bd(BLOCK_COUNT*BLOCK_SIZE, BLOCK_SIZE);
00041     uint8_t *write_block = new uint8_t[BLOCK_SIZE];
00042     uint8_t *read_block = new uint8_t[BLOCK_SIZE];
00043 
00044     // Test with first slice of block device
00045     SlicingBlockDevice slice1(&bd, 0, (BLOCK_COUNT/2)*BLOCK_SIZE);
00046 
00047     int err = slice1.init();
00048     TEST_ASSERT_EQUAL(0, err);
00049 
00050     TEST_ASSERT_EQUAL(BLOCK_SIZE, slice1.get_program_size());
00051     TEST_ASSERT_EQUAL((BLOCK_COUNT/2)*BLOCK_SIZE, slice1.size());
00052 
00053     // Fill with random sequence
00054     srand(1);
00055     for (int i = 0; i < BLOCK_SIZE; i++) {
00056         write_block[i] = 0xff & rand();
00057     }
00058 
00059     // Write, sync, and read the block
00060     err = slice1.program(write_block, 0, BLOCK_SIZE);
00061     TEST_ASSERT_EQUAL(0, err);
00062 
00063     err = slice1.read(read_block, 0, BLOCK_SIZE);
00064     TEST_ASSERT_EQUAL(0, err);
00065 
00066     // Check that the data was unmodified
00067     srand(1);
00068     for (int i = 0; i < BLOCK_SIZE; i++) {
00069         TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
00070     }
00071 
00072     // Check with original block device
00073     err = bd.read(read_block, 0, BLOCK_SIZE);
00074     TEST_ASSERT_EQUAL(0, err);
00075 
00076     // Check that the data was unmodified
00077     srand(1);
00078     for (int i = 0; i < BLOCK_SIZE; i++) {
00079         TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
00080     }
00081 
00082     err = slice1.deinit();
00083     TEST_ASSERT_EQUAL(0, err);
00084 
00085 
00086     // Test with second slice of block device
00087     SlicingBlockDevice slice2(&bd, -(BLOCK_COUNT/2)*BLOCK_SIZE);
00088 
00089     err = slice2.init();
00090     TEST_ASSERT_EQUAL(0, err);
00091 
00092     TEST_ASSERT_EQUAL(BLOCK_SIZE, slice2.get_program_size());
00093     TEST_ASSERT_EQUAL((BLOCK_COUNT/2)*BLOCK_SIZE, slice2.size());
00094 
00095     // Fill with random sequence
00096     srand(1);
00097     for (int i = 0; i < BLOCK_SIZE; i++) {
00098         write_block[i] = 0xff & rand();
00099     }
00100 
00101     // Write, sync, and read the block
00102     err = slice2.program(write_block, 0, BLOCK_SIZE);
00103     TEST_ASSERT_EQUAL(0, err);
00104 
00105     err = slice2.read(read_block, 0, BLOCK_SIZE);
00106     TEST_ASSERT_EQUAL(0, err);
00107 
00108     // Check that the data was unmodified
00109     srand(1);
00110     for (int i = 0; i < BLOCK_SIZE; i++) {
00111         TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
00112     }
00113 
00114     // Check with original block device
00115     err = bd.read(read_block, (BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE);
00116     TEST_ASSERT_EQUAL(0, err);
00117 
00118     // Check that the data was unmodified
00119     srand(1);
00120     for (int i = 0; i < BLOCK_SIZE; i++) {
00121         TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
00122     }
00123 
00124     delete[] write_block;
00125     delete[] read_block;
00126     err = slice2.deinit();
00127     TEST_ASSERT_EQUAL(0, err);
00128 }
00129 
00130 // Simple test which read/writes blocks on a chain of block devices
00131 void test_chaining() {
00132     HeapBlockDevice bd1((BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE);
00133     HeapBlockDevice bd2((BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE);
00134     uint8_t *write_block = new uint8_t[BLOCK_SIZE];
00135     uint8_t *read_block = new uint8_t[BLOCK_SIZE];
00136 
00137     // Test with chain of block device
00138     BlockDevice *bds[] = {&bd1, &bd2};
00139     ChainingBlockDevice chain(bds);
00140 
00141     int err = chain.init();
00142     TEST_ASSERT_EQUAL(0, err);
00143 
00144     TEST_ASSERT_EQUAL(BLOCK_SIZE, chain.get_program_size());
00145     TEST_ASSERT_EQUAL(BLOCK_COUNT*BLOCK_SIZE, chain.size());
00146 
00147     // Fill with random sequence
00148     srand(1);
00149     for (int i = 0; i < BLOCK_SIZE; i++) {
00150         write_block[i] = 0xff & rand();
00151     }
00152 
00153     // Write, sync, and read the block
00154     err = chain.program(write_block, 0, BLOCK_SIZE);
00155     TEST_ASSERT_EQUAL(0, err);
00156 
00157     err = chain.read(read_block, 0, BLOCK_SIZE);
00158     TEST_ASSERT_EQUAL(0, err);
00159 
00160     // Check that the data was unmodified
00161     srand(1);
00162     for (int i = 0; i < BLOCK_SIZE; i++) {
00163         TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
00164     }
00165 
00166     // Write, sync, and read the block
00167     err = chain.program(write_block, (BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE);
00168     TEST_ASSERT_EQUAL(0, err);
00169 
00170     err = chain.read(read_block, (BLOCK_COUNT/2)*BLOCK_SIZE, BLOCK_SIZE);
00171     TEST_ASSERT_EQUAL(0, err);
00172 
00173     // Check that the data was unmodified
00174     srand(1);
00175     for (int i = 0; i < BLOCK_SIZE; i++) {
00176         TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
00177     }
00178 
00179     delete[] write_block;
00180     delete[] read_block;
00181     err = chain.deinit();
00182     TEST_ASSERT_EQUAL(0, err);
00183 }
00184 
00185 // Simple test which read/writes blocks on a chain of block devices
00186 void test_profiling() {
00187     HeapBlockDevice bd(BLOCK_COUNT*BLOCK_SIZE, BLOCK_SIZE);
00188     uint8_t *write_block = new uint8_t[BLOCK_SIZE];
00189     uint8_t *read_block = new uint8_t[BLOCK_SIZE];
00190 
00191     // Test under profiling
00192     ProfilingBlockDevice profiler(&bd);
00193 
00194     int err = profiler.init();
00195     TEST_ASSERT_EQUAL(0, err);
00196 
00197     TEST_ASSERT_EQUAL(BLOCK_SIZE, profiler.get_erase_size());
00198     TEST_ASSERT_EQUAL(BLOCK_COUNT*BLOCK_SIZE, profiler.size());
00199 
00200     // Fill with random sequence
00201     srand(1);
00202     for (int i = 0; i < BLOCK_SIZE; i++) {
00203         write_block[i] = 0xff & rand();
00204     }
00205 
00206     // Write, sync, and read the block
00207     err = profiler.erase(0, BLOCK_SIZE);
00208     TEST_ASSERT_EQUAL(0, err);
00209 
00210     err = profiler.program(write_block, 0, BLOCK_SIZE);
00211     TEST_ASSERT_EQUAL(0, err);
00212 
00213     err = profiler.read(read_block, 0, BLOCK_SIZE);
00214     TEST_ASSERT_EQUAL(0, err);
00215 
00216     // Check that the data was unmodified
00217     srand(1);
00218     for (int i = 0; i < BLOCK_SIZE; i++) {
00219         TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
00220     }
00221 
00222     // Check with original block device
00223     err = bd.read(read_block, 0, BLOCK_SIZE);
00224     TEST_ASSERT_EQUAL(0, err);
00225 
00226     // Check that the data was unmodified
00227     srand(1);
00228     for (int i = 0; i < BLOCK_SIZE; i++) {
00229         TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
00230     }
00231 
00232     delete[] write_block;
00233     delete[] read_block;
00234     err = profiler.deinit();
00235     TEST_ASSERT_EQUAL(0, err);
00236 
00237     // Check that profiled operations match expectations
00238     bd_size_t read_count = profiler.get_read_count();
00239     TEST_ASSERT_EQUAL(BLOCK_SIZE, read_count);
00240     bd_size_t program_count = profiler.get_program_count();
00241     TEST_ASSERT_EQUAL(BLOCK_SIZE, program_count);
00242     bd_size_t erase_count = profiler.get_erase_count();
00243     TEST_ASSERT_EQUAL(BLOCK_SIZE, erase_count);
00244 }
00245 
00246 
00247 // Test setup
00248 utest::v1::status_t test_setup(const size_t number_of_cases) {
00249     GREENTEA_SETUP(10, "default_auto");
00250     return verbose_test_setup_handler(number_of_cases);
00251 }
00252 
00253 Case cases[] = {
00254     Case("Testing slicing of a block device", test_slicing),
00255     Case("Testing chaining of block devices", test_chaining),
00256     Case("Testing profiling of block devices", test_profiling),
00257 };
00258 
00259 Specification specification(test_setup, cases);
00260 
00261 int main() {
00262     return !Harness::run(specification);
00263 }