brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.1 KiB · 2862b14 Raw
69 lines · cpp
1//===-- Unittests for functions from POSIX dirent.h -----------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/__support/CPP/string_view.h"10#include "src/dirent/closedir.h"11#include "src/dirent/dirfd.h"12#include "src/dirent/opendir.h"13#include "src/dirent/readdir.h"14 15#include "test/UnitTest/ErrnoCheckingTest.h"16#include "test/UnitTest/Test.h"17 18#include <dirent.h>19 20using LlvmLibcDirentTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;21using string_view = LIBC_NAMESPACE::cpp::string_view;22 23TEST_F(LlvmLibcDirentTest, SimpleOpenAndRead) {24  ::DIR *dir = LIBC_NAMESPACE::opendir("testdata");25  ASSERT_TRUE(dir != nullptr);26  // The file descriptors 0, 1 and 2 are reserved for standard streams.27  // So, the file descriptor for the newly opened directory should be28  // greater than 2.29  ASSERT_GT(LIBC_NAMESPACE::dirfd(dir), 2);30 31  struct ::dirent *file1 = nullptr, *file2 = nullptr, *dir1 = nullptr,32                  *dir2 = nullptr;33  while (true) {34    struct ::dirent *d = LIBC_NAMESPACE::readdir(dir);35    if (d == nullptr)36      break;37    if (string_view(&d->d_name[0]) == "file1.txt")38      file1 = d;39    if (string_view(&d->d_name[0]) == "file2.txt")40      file2 = d;41    if (string_view(&d->d_name[0]) == "dir1")42      dir1 = d;43    if (string_view(&d->d_name[0]) == "dir2")44      dir2 = d;45  }46 47  // Verify that we don't break out of the above loop in error.48  ASSERT_ERRNO_SUCCESS();49 50  ASSERT_TRUE(file1 != nullptr);51  ASSERT_TRUE(file2 != nullptr);52  ASSERT_TRUE(dir1 != nullptr);53  ASSERT_TRUE(dir2 != nullptr);54 55  ASSERT_EQ(LIBC_NAMESPACE::closedir(dir), 0);56}57 58TEST_F(LlvmLibcDirentTest, OpenNonExistentDir) {59  ::DIR *dir = LIBC_NAMESPACE::opendir("___xyz123__.non_existent__");60  ASSERT_TRUE(dir == nullptr);61  ASSERT_ERRNO_EQ(ENOENT);62}63 64TEST_F(LlvmLibcDirentTest, OpenFile) {65  ::DIR *dir = LIBC_NAMESPACE::opendir("testdata/file1.txt");66  ASSERT_TRUE(dir == nullptr);67  ASSERT_ERRNO_EQ(ENOTDIR);68}69