|
| 1 | +#include "mbed.h" |
| 2 | +#include "greentea-client/test_env.h" |
| 3 | +#include "unity.h" |
| 4 | +#include "utest.h" |
| 5 | + |
| 6 | +#include "HeapBlockDevice.h" |
| 7 | +#include "FATFileSystem.h" |
| 8 | +#include <stdlib.h> |
| 9 | + |
| 10 | +using namespace utest::v1; |
| 11 | + |
| 12 | +// Test block device |
| 13 | +#define BLOCK_SIZE 512 |
| 14 | +HeapBlockDevice bd(128*BLOCK_SIZE, BLOCK_SIZE); |
| 15 | + |
| 16 | + |
| 17 | +void test_format() { |
| 18 | + int err = FATFileSystem::format(&bd); |
| 19 | + TEST_ASSERT_EQUAL(0, err); |
| 20 | +} |
| 21 | + |
| 22 | +template <ssize_t TEST_SIZE> |
| 23 | +void test_read_write() { |
| 24 | + FATFileSystem fs("fat"); |
| 25 | + |
| 26 | + int err = fs.mount(&bd); |
| 27 | + TEST_ASSERT_EQUAL(0, err); |
| 28 | + |
| 29 | + uint8_t *buffer = (uint8_t *)malloc(TEST_SIZE); |
| 30 | + TEST_ASSERT(buffer); |
| 31 | + |
| 32 | + // Fill with random sequence |
| 33 | + srand(1); |
| 34 | + for (int i = 0; i < TEST_SIZE; i++) { |
| 35 | + buffer[i] = 0xff & rand(); |
| 36 | + } |
| 37 | + |
| 38 | + // write and read file |
| 39 | + FileHandle *file = fs.open("test_read_write.dat", O_WRONLY | O_CREAT); |
| 40 | + TEST_ASSERT(file); |
| 41 | + ssize_t size = file->write(buffer, TEST_SIZE); |
| 42 | + TEST_ASSERT_EQUAL(TEST_SIZE, size); |
| 43 | + err = file->close(); |
| 44 | + TEST_ASSERT_EQUAL(0, err); |
| 45 | + |
| 46 | + file = fs.open("test_read_write.dat", O_RDONLY); |
| 47 | + TEST_ASSERT(file); |
| 48 | + size = file->read(buffer, TEST_SIZE); |
| 49 | + TEST_ASSERT_EQUAL(TEST_SIZE, size); |
| 50 | + err = file->close(); |
| 51 | + TEST_ASSERT_EQUAL(0, err); |
| 52 | + |
| 53 | + // Check that the data was unmodified |
| 54 | + srand(1); |
| 55 | + for (int i = 0; i < TEST_SIZE; i++) { |
| 56 | + TEST_ASSERT_EQUAL(0xff & rand(), buffer[i]); |
| 57 | + } |
| 58 | + |
| 59 | + err = fs.unmount(); |
| 60 | + TEST_ASSERT_EQUAL(0, err); |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +// Test setup |
| 65 | +utest::v1::status_t test_setup(const size_t number_of_cases) { |
| 66 | + GREENTEA_SETUP(10, "default_auto"); |
| 67 | + return verbose_test_setup_handler(number_of_cases); |
| 68 | +} |
| 69 | + |
| 70 | +Case cases[] = { |
| 71 | + Case("Testing formating", test_format), |
| 72 | + Case("Testing read write < block", test_read_write<BLOCK_SIZE/2>), |
| 73 | + Case("Testing read write > block", test_read_write<2*BLOCK_SIZE>), |
| 74 | +}; |
| 75 | + |
| 76 | +Specification specification(test_setup, cases); |
| 77 | + |
| 78 | +int main() { |
| 79 | + return !Harness::run(specification); |
| 80 | +} |
0 commit comments