🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
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.

160 lines
3.7 KiB

  1. /*
  2. * Test program to make sure that dirent compiles cleanly with C++
  3. *
  4. * Copyright (C) 1998-2019 Toni Ronkko
  5. * This file is part of dirent. Dirent may be freely distributed
  6. * under the MIT license. For all details and documentation, see
  7. * https://github.com/tronkko/dirent
  8. */
  9. #include <iostream>
  10. #include <string.h>
  11. #include <dirent.h>
  12. #include <assert.h>
  13. using namespace std;
  14. /* Filter and sort functions */
  15. static int only_readme (const struct dirent *entry);
  16. int
  17. main(
  18. int argc, char *argv[])
  19. {
  20. (void) argc;
  21. (void) argv;
  22. /* Basic directory retrieval */
  23. {
  24. DIR *dir;
  25. struct dirent *ent;
  26. int found = 0;
  27. /* Open directory */
  28. dir = opendir ("tests/1");
  29. if (dir == NULL) {
  30. cerr << "Directory tests/1 not found" << endl;
  31. abort ();
  32. }
  33. /* Read entries */
  34. while ((ent = readdir (dir)) != NULL) {
  35. /* Check each file */
  36. if (strcmp (ent->d_name, ".") == 0) {
  37. /* Directory itself */
  38. #ifdef _DIRENT_HAVE_D_TYPE
  39. assert (ent->d_type == DT_DIR);
  40. #endif
  41. #ifdef _DIRENT_HAVE_D_NAMLEN
  42. assert (ent->d_namlen == 1);
  43. #endif
  44. #ifdef _D_EXACT_NAMLEN
  45. assert (_D_EXACT_NAMLEN(ent) == 1);
  46. #endif
  47. #ifdef _D_ALLOC_NAMLEN
  48. assert (_D_ALLOC_NAMLEN(ent) > 1);
  49. #endif
  50. found += 1;
  51. } else if (strcmp (ent->d_name, "..") == 0) {
  52. /* Parent directory */
  53. #ifdef _DIRENT_HAVE_D_TYPE
  54. assert (ent->d_type == DT_DIR);
  55. #endif
  56. #ifdef _DIRENT_HAVE_D_NAMLEN
  57. assert (ent->d_namlen == 2);
  58. #endif
  59. #ifdef _D_EXACT_NAMLEN
  60. assert (_D_EXACT_NAMLEN(ent) == 2);
  61. #endif
  62. #ifdef _D_ALLOC_NAMLEN
  63. assert (_D_ALLOC_NAMLEN(ent) > 2);
  64. #endif
  65. found += 2;
  66. } else if (strcmp (ent->d_name, "file") == 0) {
  67. /* Regular file */
  68. #ifdef _DIRENT_HAVE_D_TYPE
  69. assert (ent->d_type == DT_REG);
  70. #endif
  71. #ifdef _DIRENT_HAVE_D_NAMLEN
  72. assert (ent->d_namlen == 4);
  73. #endif
  74. #ifdef _D_EXACT_NAMLEN
  75. assert (_D_EXACT_NAMLEN(ent) == 4);
  76. #endif
  77. #ifdef _D_ALLOC_NAMLEN
  78. assert (_D_ALLOC_NAMLEN(ent) > 4);
  79. #endif
  80. found += 4;
  81. } else if (strcmp (ent->d_name, "dir") == 0) {
  82. /* Just a directory */
  83. #ifdef _DIRENT_HAVE_D_TYPE
  84. assert (ent->d_type == DT_DIR);
  85. #endif
  86. #ifdef _DIRENT_HAVE_D_NAMLEN
  87. assert (ent->d_namlen == 3);
  88. #endif
  89. #ifdef _D_EXACT_NAMLEN
  90. assert (_D_EXACT_NAMLEN(ent) == 3);
  91. #endif
  92. #ifdef _D_ALLOC_NAMLEN
  93. assert (_D_ALLOC_NAMLEN(ent) > 3);
  94. #endif
  95. found += 8;
  96. } else {
  97. /* Other file */
  98. cerr << "Unexpected file " << ent->d_name << endl;
  99. abort ();
  100. }
  101. }
  102. /* Make sure that all files were found */
  103. assert (found == 0xf);
  104. closedir (dir);
  105. }
  106. /* Basic scan with simple filter function */
  107. {
  108. struct dirent **files;
  109. int n;
  110. int i;
  111. /* Read directory entries */
  112. n = scandir ("tests/3", &files, only_readme, alphasort);
  113. assert (n == 1);
  114. /* Make sure that the filter works */
  115. assert (strcmp (files[0]->d_name, "README.txt") == 0);
  116. /* Release file names */
  117. for (i = 0; i < n; i++) {
  118. free (files[i]);
  119. }
  120. free (files);
  121. }
  122. cout << "OK" << endl;
  123. return EXIT_SUCCESS;
  124. }
  125. /* Only pass README.txt file */
  126. static int
  127. only_readme (const struct dirent *entry)
  128. {
  129. int pass;
  130. if (strcmp (entry->d_name, "README.txt") == 0) {
  131. pass = 1;
  132. } else {
  133. pass = 0;
  134. }
  135. return pass;
  136. }