1 //===-- Unittests for write -----------------------------------------------===//
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "include/errno.h"
10 #include "src/unistd/write.h"
11 #include "test/ErrnoSetterMatcher.h"
12 #include "utils/UnitTest/Test.h"
13 #include "utils/testutils/FDReader.h"
14 
TEST(UniStd,WriteBasic)15 TEST(UniStd, WriteBasic) {
16   using __llvm_libc::testing::ErrnoSetterMatcher::Succeeds;
17   constexpr const char *hello = "hello";
18   __llvm_libc::testutils::FDReader reader;
19   EXPECT_THAT(__llvm_libc::write(reader.getWriteFD(), hello, 5), Succeeds(5));
20   EXPECT_TRUE(reader.matchWritten(hello));
21 }
22 
TEST(UniStd,WriteFails)23 TEST(UniStd, WriteFails) {
24   using __llvm_libc::testing::ErrnoSetterMatcher::Fails;
25 
26   EXPECT_THAT(__llvm_libc::write(-1, "", 1), Fails(EBADF));
27   EXPECT_THAT(__llvm_libc::write(1, reinterpret_cast<const void *>(-1), 1),
28               Fails(EFAULT));
29 }
30