Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

semihost_fs.cpp 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "TestHarness.h"
  2. #include "mbed.h"
  3. #include "semihost_api.h"
  4. #include <stdio.h>
  5. #define FILENAME "/local/out.txt"
  6. #define TEST_STRING "Hello World!"
  7. TEST_GROUP(FirstTestGroup)
  8. {
  9. FILE *test_open(const char *mode) {
  10. FILE *f = fopen(FILENAME, mode);
  11. return f;
  12. }
  13. bool test_write(FILE *f, char *str, int str_len) {
  14. int n = fprintf(f, str);
  15. return (n == str_len) ? true : false;
  16. }
  17. bool test_read(FILE *f, char *str, int str_len) {
  18. int n = fread(str, sizeof(unsigned char), str_len, f);
  19. return (n == str_len) ? true : false;
  20. }
  21. bool test_close(FILE *f) {
  22. int rc = fclose(f);
  23. return rc ? true : false;
  24. }
  25. };
  26. TEST(FirstTestGroup, FirstTest)
  27. {
  28. CHECK_TEXT(semihost_connected(), "Semihost not connected")
  29. LocalFileSystem local("local");
  30. char *str = TEST_STRING;
  31. char *buffer = (char *)malloc(sizeof(unsigned char) * strlen(TEST_STRING));
  32. int str_len = strlen(TEST_STRING);
  33. CHECK_TEXT(buffer != NULL, "Buffer allocation failed");
  34. CHECK_TEXT(str_len > 0, "Test string is empty (len <= 0)");
  35. {
  36. // Perform write / read tests
  37. FILE *f = NULL;
  38. // Write
  39. f = test_open("w");
  40. CHECK_TEXT(f != NULL, "Error opening file for writing")
  41. CHECK_TEXT(test_write(f, str, str_len), "Error writing file");
  42. CHECK_TEXT(test_close(f) != EOF, "Error closing file after write");
  43. // Read
  44. f = test_open("r");
  45. CHECK_TEXT(f != NULL, "Error opening file for reading")
  46. CHECK_TEXT(test_read(f, buffer, str_len), "Error reading file");
  47. CHECK_TEXT(test_close(f) != EOF, "Error closing file after read");
  48. }
  49. CHECK(strncmp(buffer, str, str_len) == 0);
  50. }