|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2017 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 "utest/utest.h" |
| 19 | +#include "unity/unity.h" |
| 20 | +#include "FileStream.h" |
| 21 | + |
| 22 | + |
| 23 | +using utest::v1::Case; |
| 24 | + |
| 25 | +#define STRINGS_SIZE 5 |
| 26 | +const char *test_strings[STRINGS_SIZE] = { "one", "two", "three", "four", "five" }; |
| 27 | + |
| 28 | + |
| 29 | +/** Test Stream interface: printf/scanf |
| 30 | +
|
| 31 | + Given a FileStream object created |
| 32 | + When put some strings to file stream via printf |
| 33 | + Then it can be read from file stream via scanf |
| 34 | + */ |
| 35 | +void test_printf_scanf() |
| 36 | +{ |
| 37 | + char str[16]; |
| 38 | + const uint32_t file_max_size = 128; |
| 39 | + FileStream<file_max_size> file("printf_scanf_test_file"); |
| 40 | + |
| 41 | + for(int i = 0; i < STRINGS_SIZE; i++) { |
| 42 | + int ret = file.printf("%s ", test_strings[i]); |
| 43 | + } |
| 44 | + |
| 45 | + fseek((std::FILE*)file, SEEK_SET, 0); |
| 46 | + |
| 47 | + for(int i = 0; i < STRINGS_SIZE; i++) { |
| 48 | + file.scanf("%s", str); |
| 49 | + TEST_ASSERT_EQUAL_STRING(test_strings[i], str); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/** Test Stream interface: puts/gets |
| 54 | +
|
| 55 | + Given a FileStream object created |
| 56 | + When put some strings to file stream via puts |
| 57 | + Then it can be read from file stream via gets |
| 58 | + */ |
| 59 | +void test_puts_gets() |
| 60 | +{ |
| 61 | + char str[16]; |
| 62 | + const uint32_t file_max_size = 128; |
| 63 | + FileStream<file_max_size> file("_puts_gets_test_file"); |
| 64 | + |
| 65 | + for(int i = 0; i < STRINGS_SIZE; i++) { |
| 66 | + file.puts(test_strings[i]); |
| 67 | + } |
| 68 | + |
| 69 | + fseek((std::FILE*)file, 0, SEEK_SET); |
| 70 | + |
| 71 | + for(int i = 0; i < STRINGS_SIZE; i++) { |
| 72 | + TEST_ASSERT_EQUAL_STRING(test_strings[i], file.gets(str, strlen(test_strings[i]) + 1)); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/** Test Stream interface: putc/getc |
| 77 | +
|
| 78 | + Given a FileStream object created |
| 79 | + When put some characters to file stream via putc |
| 80 | + Then it can be read from file stream via getc |
| 81 | + */ |
| 82 | +void test_putc_getc() |
| 83 | +{ |
| 84 | + const uint32_t file_max_size = 32; |
| 85 | + char chars[] = { 'a', 'b', 'c' }; |
| 86 | + const uint32_t count = sizeof(chars); |
| 87 | + FileStream<file_max_size> file("test_file"); |
| 88 | + |
| 89 | + for(int i = 0; i < count; i++) { |
| 90 | + file.putc(chars[i]); |
| 91 | + } |
| 92 | + |
| 93 | + fseek((std::FILE*)file, 0, SEEK_SET); |
| 94 | + |
| 95 | + for(int i = 0; i < count; i++) { |
| 96 | + TEST_ASSERT_EQUAL_INT((int)chars[i], file.getc()); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/** Test FileBase interface: file list |
| 101 | +
|
| 102 | + Given a FileStream class inherited from FileBase |
| 103 | + When before any FileStream object creation |
| 104 | + Then the underlying file list is empty |
| 105 | + When the FileStream object is created |
| 106 | + Then the file is on the underlying file list and is accessible via get and lookup |
| 107 | + When the FileStream object is destroyed |
| 108 | + Then it is not present on the underlying file list |
| 109 | + */ |
| 110 | +void test_FileBase_list() |
| 111 | +{ |
| 112 | + const char *name = "test_file"; |
| 113 | + const uint32_t file_max_size = 1; |
| 114 | + |
| 115 | + FileBase *file = FileStream<file_max_size>::get(0); |
| 116 | + TEST_ASSERT_EQUAL_PTR(NULL, file); |
| 117 | + |
| 118 | + { |
| 119 | + FileStream<file_max_size> stream(name); |
| 120 | + |
| 121 | + FileBase *file1 = FileStream<file_max_size>::get(0); |
| 122 | + TEST_ASSERT_NOT_NULL(file1); |
| 123 | + |
| 124 | + FileBase *file2 = FileStream<file_max_size>::lookup(name, strlen(name)); |
| 125 | + TEST_ASSERT_EQUAL_PTR(file1, file2); |
| 126 | + } |
| 127 | + |
| 128 | + file = FileStream<file_max_size>::lookup(name, strlen(name)); |
| 129 | + TEST_ASSERT_EQUAL_PTR(NULL, file); |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | +/** Test FileBase interface: file name and file type |
| 134 | +
|
| 135 | + Given a FileStream object inherited from FileBase |
| 136 | + When check its name |
| 137 | + Then the name is same as set during construction |
| 138 | + When check its path type |
| 139 | + Then the path type is FilePathType |
| 140 | + */ |
| 141 | +void test_FileBase_name_and_type() |
| 142 | +{ |
| 143 | + const char *name = "test_file"; |
| 144 | + |
| 145 | + FileStream<1> stream(name); |
| 146 | + |
| 147 | + TEST_ASSERT_EQUAL_STRING(name, stream.getName()); |
| 148 | + |
| 149 | + TEST_ASSERT_EQUAL_MESSAGE(FilePathType, stream.getPathType(), "Wrong file type!!!"); |
| 150 | +} |
| 151 | + |
| 152 | + |
| 153 | +utest::v1::status_t test_setup(const size_t number_of_cases) |
| 154 | +{ |
| 155 | + GREENTEA_SETUP(10, "default_auto"); |
| 156 | + return utest::v1::verbose_test_setup_handler(number_of_cases); |
| 157 | +} |
| 158 | + |
| 159 | +Case cases[] = { |
| 160 | + Case("Test files list empty", test_FileBase_list), |
| 161 | + Case("Test file name and type", test_FileBase_name_and_type), |
| 162 | + Case("Test putc/getc", test_putc_getc), |
| 163 | + Case("Test puts/gets", test_puts_gets), |
| 164 | + Case("Test printf/scanf", test_printf_scanf), |
| 165 | +}; |
| 166 | + |
| 167 | +utest::v1::Specification specification(test_setup, cases); |
| 168 | + |
| 169 | +int main() |
| 170 | +{ |
| 171 | + return !utest::v1::Harness::run(specification); |
| 172 | +} |
0 commit comments