BA / SerialCom

Fork of OmniWheels by Gustav Atmel

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018 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 
00017 #include "greentea-client/test_env.h"
00018 #include "utest/utest.h"
00019 #include "unity/unity.h"
00020 #include "TestFile.h"
00021 #include "mbed.h"
00022 
00023 using utest::v1::Case;
00024 
00025 
00026 /** Test fopen and fclose
00027  *
00028  *  Given a file to be opened
00029  *
00030  *  When the file is open
00031  *  Then returned file descriptor is valid
00032  *
00033  *  When the file is closed
00034  *  Then underneath retargeting layer function is called
00035  *       and the fclose function return with succeed
00036  *
00037  */
00038 void test_fopen_fclose()
00039 {
00040     std::FILE *file;
00041     const uint32_t FS = 5;
00042     TestFile<FS> fh;
00043 
00044     file = fdopen(&fh, "w+");
00045     TEST_ASSERT_NOT_NULL(file);
00046 
00047     TestFile<FS>::resetFunctionCallHistory();
00048     int ret = std::fclose(file);
00049     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnClose));
00050     TEST_ASSERT_EQUAL_INT(0, ret);
00051 }
00052 
00053 
00054 /** Test fwrite and fread
00055  *
00056  *  Given already opened file
00057  *
00058  *  When write some data to file
00059  *  Then underneath retargeting layer write function is called
00060  *       fwrite return number of successfully written elements
00061  *       when not all elements were written stream error is set
00062  *
00063  *  When read previously written data from file
00064  *  Then underneath retargeting layer read function is called
00065  *       fread return number of successfully read elements
00066  *       read data match previously written
00067  *       when read less then expected stream eof is set
00068  *
00069  */
00070 void test_fwrite_fread()
00071 {
00072     std::FILE *file;
00073     const uint32_t FS = 5;
00074     TestFile<FS> fh;
00075     char read_buf[16];
00076     const char *str1 = "abc";
00077     const char *str2 = "def";
00078     const uint32_t str1_size = strlen(str1);
00079     const uint32_t str2_size = strlen(str2);
00080     std::size_t write_ret;
00081     std::size_t read_ret;
00082 
00083     file = fdopen(&fh, "w+");
00084     TEST_ASSERT_NOT_NULL(file);
00085     std::setbuf(file, NULL);
00086 
00087     // write 3; expected written 3
00088     TestFile<FS>::resetFunctionCallHistory();
00089     write_ret = std::fwrite(str1, 1, str1_size, file);
00090     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnWrite));
00091     TEST_ASSERT_EQUAL_INT(str1_size, write_ret);
00092 
00093     // write 3; expected written 2
00094     TestFile<FS>::resetFunctionCallHistory();
00095     write_ret = std::fwrite(str2, 1, str2_size, file);
00096     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnWrite));
00097     TEST_ASSERT_TRUE(std::ferror(file) != 0);
00098     std::clearerr(file);
00099 
00100     // ARMCC/IAR returns 0 here instead of number of elements successfully written !!!
00101     TEST_ASSERT_TRUE(write_ret >= 0 && write_ret <= (str2_size - 1));
00102 
00103     // write 3; expected written 0
00104     TestFile<FS>::resetFunctionCallHistory();
00105     write_ret = std::fwrite(str1, 1, str1_size, file);
00106     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnWrite));
00107     TEST_ASSERT_TRUE(std::ferror(file) != 0);
00108     TEST_ASSERT_EQUAL_INT(0, write_ret);
00109 
00110     std::rewind(file);
00111 
00112     // read 3; expected read 3
00113     TestFile<FS>::resetFunctionCallHistory();
00114     read_ret = std::fread(read_buf, 1, str1_size, file);
00115     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnRead));
00116     TEST_ASSERT_EQUAL_INT(str1_size, read_ret);
00117     TEST_ASSERT_EQUAL_INT(0, strncmp(str1, read_buf, str1_size));
00118 
00119     // read 3; expected read 2
00120     TestFile<FS>::resetFunctionCallHistory();
00121     read_ret = std::fread(read_buf, 1, str2_size, file);
00122     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnRead));
00123     TEST_ASSERT_TRUE(std::feof(file) != 0);
00124     std::clearerr(file);
00125     TEST_ASSERT_EQUAL_INT(str2_size - 1, read_ret);
00126     TEST_ASSERT_EQUAL_INT(0, strncmp(str2, read_buf, str2_size - 1));
00127 
00128     // read 3; expected read 0
00129     TestFile<FS>::resetFunctionCallHistory();
00130     read_ret = std::fread(read_buf, 1, str2_size, file);
00131     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnRead));
00132     TEST_ASSERT_TRUE(std::feof(file) != 0);
00133     TEST_ASSERT_EQUAL_INT(0, read_ret);
00134 
00135     std::fclose(file);
00136 }
00137 
00138 
00139 
00140 /** Test fputc and fgetc
00141  *
00142  *  Given already opened file
00143  *
00144  *  When write some data to file
00145  *  Then underneath retargeting layer write function is called
00146  *       fputc return written element
00147  *       on failure, returns EOF and stream error is sets
00148  *
00149  *  When read previously written data from file
00150  *  Then underneath retargeting layer read function is called
00151  *       fgetc return read element
00152  *       read data match previously written
00153  *       on failure, returns EOF and stream error/eof is sets respectively
00154  *
00155  */
00156 void test_fputc_fgetc()
00157 {
00158     std::FILE *file;
00159     const uint32_t FS = 3;
00160     TestFile<FS> fh;
00161     char char_buf[3] = {'a', 'b', 'c' };
00162     int ret;
00163 
00164     file = fdopen(&fh, "w+");
00165     TEST_ASSERT_NOT_NULL(file);
00166     std::setbuf(file, NULL);
00167 
00168     // write 1; expected written 1
00169     TestFile<FS>::resetFunctionCallHistory();
00170     ret = std::fputc(char_buf[0], file);
00171     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnWrite));
00172     TEST_ASSERT_EQUAL_INT(char_buf[0], ret);
00173 
00174     // write 1; expected written 1
00175     TestFile<FS>::resetFunctionCallHistory();
00176     ret = std::fputc(char_buf[1], file);
00177     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnWrite));
00178     TEST_ASSERT_EQUAL_INT(char_buf[1], ret);
00179 
00180     // write 1; expected written 1
00181     TestFile<FS>::resetFunctionCallHistory();
00182     ret = std::fputc(char_buf[2], file);
00183     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnWrite));
00184     TEST_ASSERT_EQUAL_INT(char_buf[2], ret);
00185 
00186     // write 1; expected written 0
00187     TestFile<FS>::resetFunctionCallHistory();
00188     ret = std::fputc(char_buf[0], file);
00189     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnWrite));
00190     TEST_ASSERT_TRUE(std::ferror(file) != 0);
00191     TEST_ASSERT_EQUAL_INT(EOF, ret);
00192 
00193     std::rewind(file);
00194 
00195     // read 1; expected read 1
00196     TestFile<FS>::resetFunctionCallHistory();
00197     ret = std::fgetc(file);
00198     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnRead));
00199     TEST_ASSERT_EQUAL_INT(char_buf[0], ret);
00200 
00201     // read 1; expected read 1
00202     TestFile<FS>::resetFunctionCallHistory();
00203     ret = std::fgetc(file);
00204     TEST_ASSERT_EQUAL_INT(char_buf[1], ret);
00205 
00206     // read 1; expected read 1
00207     TestFile<FS>::resetFunctionCallHistory();
00208     ret = std::fgetc(file);
00209     TEST_ASSERT_EQUAL_INT(char_buf[2], ret);
00210 
00211     // read 1; expected read 0
00212     TestFile<FS>::resetFunctionCallHistory();
00213     ret = std::fgetc(file);
00214     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnRead));
00215     TEST_ASSERT_TRUE(std::feof(file) != 0);
00216     TEST_ASSERT_EQUAL_INT(EOF, ret);
00217 
00218     std::fclose(file);
00219 }
00220 
00221 /** Test fputs and fgets
00222  *
00223  *  Given already opened file
00224  *
00225  *  When write some data to file
00226  *  Then underneath retargeting layer write function is called
00227  *       on success, returns a non-negative value
00228  *       on failure, returns EOF and set stream error
00229  *
00230  *  When read previously written data from file
00231  *  Then underneath retargeting layer read function is called
00232  *       fgets return valid buffer, and read data match previously written
00233  *       when read less then expected stream EOF is set
00234  *       on failure, stream error is sets
00235  *
00236  */
00237 void test_fputs_fgets()
00238 {
00239     std::FILE *file;
00240     const uint32_t FS = 5;
00241     TestFile<FS> fh;
00242     const char *str1 = "abc";
00243     const char *str2 = "def";
00244     const uint32_t str1_size = strlen(str1);
00245     const uint32_t str2_size = strlen(str2);
00246     char read_buf[16];
00247     int fputs_ret;
00248     char *fgets_ret;
00249 
00250     file = fdopen(&fh, "w+");
00251     TEST_ASSERT_NOT_NULL(file);
00252     std::setbuf(file, NULL);
00253 
00254     // write 3; expected written 3
00255     TestFile<FS>::resetFunctionCallHistory();
00256     fputs_ret = std::fputs(str1, file);
00257     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnWrite));
00258     TEST_ASSERT_TRUE(fputs_ret >= 0);
00259 
00260     // write 3; expected written 2
00261     TestFile<FS>::resetFunctionCallHistory();
00262     fputs_ret = std::fputs(str2, file);
00263     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnWrite));
00264     TEST_ASSERT_TRUE(std::ferror(file) != 0);
00265     std::clearerr(file);
00266     TEST_ASSERT_EQUAL_INT(EOF, fputs_ret);
00267 
00268     // write 3; expected written 0
00269     TestFile<FS>::resetFunctionCallHistory();
00270     fputs_ret = std::fputs(str1, file);
00271     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnWrite));
00272     TEST_ASSERT_TRUE(std::ferror(file) != 0);
00273     TEST_ASSERT_EQUAL_INT(EOF, fputs_ret);
00274 
00275     std::rewind(file);
00276 
00277     // read 3; expected read 3
00278     TestFile<FS>::resetFunctionCallHistory();
00279     fgets_ret = std::fgets(read_buf, str1_size + 1, file);
00280     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnRead));
00281     TEST_ASSERT_EQUAL_INT(read_buf, fgets_ret);
00282     TEST_ASSERT_EQUAL_INT(0, strncmp(read_buf, str1, str1_size));
00283 
00284     // read 3; expected read 2
00285     TestFile<FS>::resetFunctionCallHistory();
00286     fgets_ret = std::fgets(read_buf, str2_size + 1, file);
00287     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnRead));
00288     TEST_ASSERT_TRUE(std::feof(file) != 0);
00289     std::clearerr(file);
00290     TEST_ASSERT_EQUAL_INT(read_buf, fgets_ret);
00291     TEST_ASSERT_EQUAL_INT(0, strncmp(read_buf, str2, str2_size - 2));
00292 
00293     // read 3; expected read 0
00294     TestFile<FS>::resetFunctionCallHistory();
00295     fgets_ret = std::fgets(read_buf, str2_size + 1, file);
00296     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnRead));
00297     TEST_ASSERT_TRUE(std::feof(file) != 0);
00298     TEST_ASSERT_EQUAL_INT(NULL, fgets_ret);
00299 
00300     std::fclose(file);
00301 }
00302 
00303 /** Test fprintf and fscanf
00304  *
00305  *  Given already opened file
00306  *
00307  *  When write some data to file
00308  *  Then underneath retargeting layer write function is called
00309  *       fprintf return number of written components
00310  *       fprintf return negative value if an error occurred and set stream error
00311  *
00312  *  When read previously written data from file
00313  *  Then underneath retargeting layer read function is called
00314  *       fscanf return number of read components, and read data match previously written
00315  *       when read less then expected stream EOF is set
00316  *       on failure, stream error is sets
00317  *
00318  */
00319 void test_fprintf_fscanf()
00320 {
00321     std::FILE *file;
00322     const uint32_t FS = 5;
00323     TestFile<FS> fh;
00324     const char *str1 = "abc";
00325     const char *str2 = "def";
00326     const uint32_t str1_size = strlen(str1);
00327     const uint32_t str2_size = strlen(str2);
00328     char read_buf[16];
00329     int fprintf_ret;
00330     int fscanf_ret;
00331 
00332     file = fdopen(&fh, "w+");
00333     TEST_ASSERT_NOT_NULL(file);
00334     std::setbuf(file, NULL);
00335 
00336     // write 3; expected written 3
00337     TestFile<FS>::resetFunctionCallHistory();
00338     fprintf_ret = fprintf(file, "%s", str1);
00339     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnWrite));
00340     TEST_ASSERT_EQUAL_INT(str1_size, fprintf_ret);
00341 
00342     // write 3; expected written 2
00343     TestFile<FS>::resetFunctionCallHistory();
00344     fprintf_ret = fprintf(file, "%s", str2);
00345     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnWrite));
00346     TEST_ASSERT_TRUE(std::ferror(file) != 0);
00347     std::clearerr(file);
00348     TEST_ASSERT_TRUE(fprintf_ret < 0);
00349 
00350     // write 3; expected written 0
00351     TestFile<FS>::resetFunctionCallHistory();
00352     fprintf_ret = fprintf(file, "%s", str2);
00353     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnWrite));
00354     TEST_ASSERT_TRUE(std::ferror(file) != 0);
00355     TEST_ASSERT_TRUE(fprintf_ret < 0);
00356 
00357     std::rewind(file);
00358 
00359     // read 3; expected read 3
00360     TestFile<FS>::resetFunctionCallHistory();
00361     fscanf_ret = fscanf(file, "%3s", read_buf);
00362     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnRead));
00363     TEST_ASSERT_EQUAL_INT(1, fscanf_ret);
00364     TEST_ASSERT_EQUAL_INT(0, strncmp(read_buf, str1, str1_size));
00365 
00366     // read 3; expected read 2
00367     TestFile<FS>::resetFunctionCallHistory();
00368     fscanf_ret = fscanf(file, "%3s", read_buf);
00369     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnRead));
00370     TEST_ASSERT_TRUE(std::feof(file) != 0);
00371     std::clearerr(file);
00372     TEST_ASSERT_EQUAL_INT(1, fscanf_ret);
00373     TEST_ASSERT_EQUAL_INT(0, strncmp(read_buf, str2, str2_size - 1));
00374 
00375     // read 3; expected read 0
00376     TestFile<FS>::resetFunctionCallHistory();
00377     fscanf_ret = fscanf(file, "%3s", read_buf);
00378     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnRead));
00379     TEST_ASSERT_TRUE(std::feof(file) != 0);
00380     TEST_ASSERT_EQUAL_INT(EOF, fscanf_ret);
00381 
00382     std::fclose(file);
00383 }
00384 
00385 /** Test fseek and ftell
00386  *
00387  *  Given already opened file is empty
00388  *
00389  *  When set the file position indicator via fseek
00390  *  Then underneath retargeting layer seek function is called
00391  *       fseek return with succeed and ftell return already set position
00392  *
00393  *  Given already opened file is not empty
00394  *
00395  *  When set the file position indicator via fseek
00396  *  Then underneath retargeting layer seek function is called
00397  *       fseek return with succeed and ftell return already set position
00398  *
00399  */
00400 void test_fseek_ftell()
00401 {
00402     std::FILE *file;
00403     long ftell_ret;
00404     int fssek_ret;
00405     const uint32_t FS = 128;
00406     TestFile<FS> fh;
00407 
00408     file = fdopen(&fh, "w+");
00409     TEST_ASSERT_NOT_NULL(file);
00410     std::setbuf(file, NULL);
00411 
00412     TestFile<FS>::resetFunctionCallHistory();
00413     ftell_ret = std::ftell(file);
00414     TEST_ASSERT_EQUAL(0, ftell_ret);
00415 
00416     TestFile<FS>::resetFunctionCallHistory();
00417     fssek_ret = std::fseek(file, 0, SEEK_CUR);
00418     TEST_ASSERT_EQUAL(0, fssek_ret);
00419 
00420     TestFile<FS>::resetFunctionCallHistory();
00421     fssek_ret = std::fseek(file, 0, SEEK_SET);
00422     TEST_ASSERT_EQUAL(0, fssek_ret);
00423 
00424     TestFile<FS>::resetFunctionCallHistory();
00425     fssek_ret = std::fseek(file, 0, SEEK_END);
00426     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnSeek));
00427     TEST_ASSERT_EQUAL(0, fssek_ret);
00428 
00429     const char *str = "Hello world";
00430     const std::size_t size = std::strlen(str);
00431 
00432     std::fwrite(str, 1, size, file);
00433 
00434     TestFile<FS>::resetFunctionCallHistory();
00435     ftell_ret = std::ftell(file);
00436     TEST_ASSERT_EQUAL(size, ftell_ret);
00437 
00438     TestFile<FS>::resetFunctionCallHistory();
00439     fssek_ret = std::fseek(file, 5, SEEK_SET);
00440     TEST_ASSERT_EQUAL(0, fssek_ret);
00441     ftell_ret = std::ftell(file);
00442     TEST_ASSERT_EQUAL(5, ftell_ret);
00443 
00444     TestFile<FS>::resetFunctionCallHistory();
00445     fssek_ret = std::fseek(file, -5, SEEK_CUR);
00446     TEST_ASSERT_EQUAL(0, fssek_ret);
00447     ftell_ret = std::ftell(file);
00448     TEST_ASSERT_EQUAL(0, ftell_ret);
00449 
00450     TestFile<FS>::resetFunctionCallHistory();
00451     fssek_ret = std::fseek(file, 0, SEEK_END);
00452     TEST_ASSERT_TRUE(TestFile<FS>::functionCalled(TestFile<FS>::fnSeek));
00453     TEST_ASSERT_EQUAL(0, fssek_ret);
00454     ftell_ret = std::ftell(file);
00455     TEST_ASSERT_EQUAL(size, ftell_ret);
00456 
00457     std::fclose(file);
00458 }
00459 
00460 utest::v1::status_t test_setup(const size_t number_of_cases)
00461 {
00462     GREENTEA_SETUP(10, "default_auto");
00463     return utest::v1::verbose_test_setup_handler(number_of_cases);
00464 }
00465 
00466 Case cases[] = {
00467     Case("Test fopen/fclose", test_fopen_fclose),
00468     Case("Test fwrite/fread", test_fwrite_fread),
00469     Case("Test fputc/fgetc", test_fputc_fgetc),
00470     Case("Test fputs/fgets", test_fputs_fgets),
00471     Case("Test fprintf/fscanf", test_fprintf_fscanf),
00472     Case("Test fseek/ftell", test_fseek_ftell)
00473 };
00474 
00475 utest::v1::Specification specification(test_setup, cases);
00476 
00477 int main()
00478 {
00479     return !utest::v1::Harness::run(specification);
00480 }