test_misc.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <assert.h>
  7. #include <stdint.h>
  8. #include <stddef.h>
  9. #include <stdbool.h>
  10. #include <stdio.h>
  11. #include <unistd.h>
  12. #include <errno.h>
  13. #include <sys/param.h>
  14. #include <stdlib.h>
  15. #include "unity.h"
  16. #include "unity_fixture.h"
  17. // unity_fixture_malloc_overrides.h defines 'free' as 'unity_free',
  18. // which can only handle pointers allocated with 'unity_malloc'.
  19. // This test allocates memory via 'posix_memalign' so calling 'free'
  20. // for these pointers causes the heap guards check to fail.
  21. #undef free
  22. TEST_GROUP(misc);
  23. TEST_SETUP(misc)
  24. {
  25. }
  26. TEST_TEAR_DOWN(misc)
  27. {
  28. }
  29. TEST(misc, posix_memalign)
  30. {
  31. void* outptr;
  32. int ret;
  33. ret = posix_memalign(&outptr, 4, 0);
  34. TEST_ASSERT_EQUAL_PTR(NULL, outptr);
  35. TEST_ASSERT_EQUAL_INT(ret, 0);
  36. void* magic = (void*) 0xEFEFEFEF;
  37. outptr = magic;
  38. ret = posix_memalign(&outptr, 0x10000000, 64); // too big alignment - should fail on all targets
  39. TEST_ASSERT_EQUAL_INT(ret, ENOMEM);
  40. TEST_ASSERT_EQUAL_PTR(magic, outptr); // was not modified
  41. outptr = magic;
  42. ret = posix_memalign(&outptr, 16, 0x10000000); // too big size - should fail on all targets
  43. TEST_ASSERT_EQUAL_INT(ret, ENOMEM);
  44. TEST_ASSERT_EQUAL_PTR(magic, outptr); // was not modified
  45. outptr = magic;
  46. ret = posix_memalign(&outptr, 16, 64);
  47. TEST_ASSERT_TRUE(outptr != magic);
  48. TEST_ASSERT_NOT_NULL(outptr);
  49. TEST_ASSERT_EQUAL_INT(ret, 0);
  50. free(outptr);
  51. }
  52. TEST(misc, sysconf)
  53. {
  54. TEST_ASSERT_NOT_EQUAL(-1, sysconf(_SC_NPROCESSORS_ONLN));
  55. }
  56. TEST(misc, realpath)
  57. {
  58. char out[PATH_MAX];
  59. TEST_ASSERT_EQUAL_STRING("/", realpath("/", out));
  60. TEST_ASSERT_EQUAL_STRING("/", realpath("//", out));
  61. TEST_ASSERT_EQUAL_STRING("/", realpath(".", out));
  62. TEST_ASSERT_EQUAL_STRING("/", realpath("./", out));
  63. TEST_ASSERT_EQUAL_STRING("/", realpath("/.", out));
  64. TEST_ASSERT_EQUAL_STRING("/", realpath("./.", out));
  65. TEST_ASSERT_EQUAL_STRING("/", realpath("..", out));
  66. TEST_ASSERT_EQUAL_STRING("/", realpath("../..", out));
  67. TEST_ASSERT_EQUAL_STRING("/a", realpath("/a/", out));
  68. TEST_ASSERT_EQUAL_STRING("/", realpath("/a/..", out));
  69. TEST_ASSERT_EQUAL_STRING("/", realpath("/a/../..", out));
  70. TEST_ASSERT_EQUAL_STRING("/c", realpath("/a/../b/../c", out));
  71. TEST_ASSERT_EQUAL_STRING("/abc/def", realpath("/abc/./def/ghi/..", out));
  72. char* out_new = realpath("/abc/./def/ghi/..", NULL);
  73. TEST_ASSERT_NOT_NULL(out_new);
  74. TEST_ASSERT_EQUAL_STRING("/abc/def", out_new);
  75. free(out_new);
  76. }
  77. TEST_GROUP_RUNNER(misc)
  78. {
  79. RUN_TEST_CASE(misc, posix_memalign)
  80. RUN_TEST_CASE(misc, sysconf)
  81. RUN_TEST_CASE(misc, realpath)
  82. }