|
| 1 | +/* |
| 2 | + * Test program to make sure that dirent compiles cleanly with C++ |
| 3 | + * |
| 4 | + * Copyright (C) 2006-2012 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 | + |
| 15 | +int |
| 16 | +main( |
| 17 | + int argc, char *argv[]) |
| 18 | +{ |
| 19 | + (void) argc; |
| 20 | + (void) argv; |
| 21 | + |
| 22 | + /* Basic directory retrieval */ |
| 23 | + { |
| 24 | + DIR *dir; |
| 25 | + struct dirent *ent; |
| 26 | + int found = 0; |
| 27 | + |
| 28 | + /* Open directory */ |
| 29 | + dir = opendir ("tests/1"); |
| 30 | + if (dir == NULL) { |
| 31 | + cerr << "Directory tests/1 not found" << endl; |
| 32 | + abort (); |
| 33 | + } |
| 34 | + |
| 35 | + /* Read entries */ |
| 36 | + while ((ent = readdir (dir)) != NULL) { |
| 37 | + |
| 38 | + /* Check each file */ |
| 39 | + if (strcmp (ent->d_name, ".") == 0) { |
| 40 | + /* Directory itself */ |
| 41 | +#ifdef _DIRENT_HAVE_D_TYPE |
| 42 | + assert (ent->d_type == DT_DIR); |
| 43 | +#endif |
| 44 | +#ifdef _DIRENT_HAVE_D_NAMLEN |
| 45 | + assert (ent->d_namlen == 1); |
| 46 | +#endif |
| 47 | +#ifdef _D_EXACT_NAMLEN |
| 48 | + assert (_D_EXACT_NAMLEN(ent) == 1); |
| 49 | +#endif |
| 50 | +#ifdef _D_ALLOC_NAMLEN |
| 51 | + assert (_D_ALLOC_NAMLEN(ent) > 1); |
| 52 | +#endif |
| 53 | + found += 1; |
| 54 | + |
| 55 | + } else if (strcmp (ent->d_name, "..") == 0) { |
| 56 | + /* Parent directory */ |
| 57 | +#ifdef _DIRENT_HAVE_D_TYPE |
| 58 | + assert (ent->d_type == DT_DIR); |
| 59 | +#endif |
| 60 | +#ifdef _DIRENT_HAVE_D_NAMLEN |
| 61 | + assert (ent->d_namlen == 2); |
| 62 | +#endif |
| 63 | +#ifdef _D_EXACT_NAMLEN |
| 64 | + assert (_D_EXACT_NAMLEN(ent) == 2); |
| 65 | +#endif |
| 66 | +#ifdef _D_ALLOC_NAMLEN |
| 67 | + assert (_D_ALLOC_NAMLEN(ent) > 2); |
| 68 | +#endif |
| 69 | + found += 2; |
| 70 | + |
| 71 | + } else if (strcmp (ent->d_name, "file") == 0) { |
| 72 | + /* Regular file */ |
| 73 | +#ifdef _DIRENT_HAVE_D_TYPE |
| 74 | + assert (ent->d_type == DT_REG); |
| 75 | +#endif |
| 76 | +#ifdef _DIRENT_HAVE_D_NAMLEN |
| 77 | + assert (ent->d_namlen == 4); |
| 78 | +#endif |
| 79 | +#ifdef _D_EXACT_NAMLEN |
| 80 | + assert (_D_EXACT_NAMLEN(ent) == 4); |
| 81 | +#endif |
| 82 | +#ifdef _D_ALLOC_NAMLEN |
| 83 | + assert (_D_ALLOC_NAMLEN(ent) > 4); |
| 84 | +#endif |
| 85 | + found += 4; |
| 86 | + |
| 87 | + } else if (strcmp (ent->d_name, "dir") == 0) { |
| 88 | + /* Just a directory */ |
| 89 | +#ifdef _DIRENT_HAVE_D_TYPE |
| 90 | + assert (ent->d_type == DT_DIR); |
| 91 | +#endif |
| 92 | +#ifdef _DIRENT_HAVE_D_NAMLEN |
| 93 | + assert (ent->d_namlen == 3); |
| 94 | +#endif |
| 95 | +#ifdef _D_EXACT_NAMLEN |
| 96 | + assert (_D_EXACT_NAMLEN(ent) == 3); |
| 97 | +#endif |
| 98 | +#ifdef _D_ALLOC_NAMLEN |
| 99 | + assert (_D_ALLOC_NAMLEN(ent) > 3); |
| 100 | +#endif |
| 101 | + found += 8; |
| 102 | + |
| 103 | + } else { |
| 104 | + /* Other file */ |
| 105 | + cerr << "Unexpected file " << ent->d_name << endl; |
| 106 | + abort (); |
| 107 | + } |
| 108 | + |
| 109 | + } |
| 110 | + |
| 111 | + /* Make sure that all files were found */ |
| 112 | + assert (found == 0xf); |
| 113 | + |
| 114 | + closedir (dir); |
| 115 | + } |
| 116 | + |
| 117 | + cout << "OK" << endl; |
| 118 | + return EXIT_SUCCESS; |
| 119 | +} |
| 120 | + |
0 commit comments