2 //===-- Unittests for swab ------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #include "src/unistd/swab.h"
12 #include "src/string/string_utils.h"
13 #include "test/UnitTest/Test.h"
15 TEST(LlvmLibcSwabTest
, NegativeSizeIsNoOp
) {
16 const char *from
= "abc";
17 char to
[4] = {'x', 'y', 'z', '\0'};
18 LIBC_NAMESPACE::swab(from
, to
, -1);
19 ASSERT_STREQ(to
, "xyz");
22 TEST(LlvmLibcSwabTest
, ZeroSizeIsNoOp
) {
23 const char *from
= "abc";
24 char to
[4] = {'x', 'y', 'z', '\0'};
25 LIBC_NAMESPACE::swab(from
, to
, 0);
26 ASSERT_STREQ(to
, "xyz");
29 TEST(LlvmLibcSwabTest
, SingleByteIsNoOp
) {
31 char to
[4] = {'x', 'y', 'z', '\0'};
32 LIBC_NAMESPACE::swab(from
, to
, sizeof(from
));
33 ASSERT_STREQ(to
, "xyz");
36 TEST(LlvmLibcSwabTest
, NullPtrsAreNotDeRefedIfNIsLessThanTwo
) {
37 // This test passes if a crash does not happen
38 LIBC_NAMESPACE::swab(nullptr, nullptr, -1);
39 LIBC_NAMESPACE::swab(nullptr, nullptr, 0);
40 LIBC_NAMESPACE::swab(nullptr, nullptr, 1);
43 TEST(LlvmLibcSwabTest
, BytesAreSwappedWithEvenN
) {
45 const char *from
= "ab";
47 LIBC_NAMESPACE::swab(from
, to
,
48 LIBC_NAMESPACE::internal::string_length(from
));
49 ASSERT_STREQ(to
, "ba");
52 const char *from
= "abcd";
54 LIBC_NAMESPACE::swab(from
, to
,
55 LIBC_NAMESPACE::internal::string_length(from
));
56 ASSERT_STREQ(to
, "badc");
59 const char *from
= "aAaAaA";
61 LIBC_NAMESPACE::swab(from
, to
,
62 LIBC_NAMESPACE::internal::string_length(from
));
63 ASSERT_STREQ(to
, "AaAaAa");
67 TEST(LlvmLibcSwabTest
, LastByteIgnoredWithOddN
) {
69 const char *from
= "aba";
71 LIBC_NAMESPACE::swab(from
, to
,
72 LIBC_NAMESPACE::internal::string_length(from
));
73 ASSERT_STREQ(to
, "ba");
76 const char *from
= "abcde";
78 LIBC_NAMESPACE::swab(from
, to
,
79 LIBC_NAMESPACE::internal::string_length(from
));
80 ASSERT_STREQ(to
, "badc");
83 const char *from
= "aAaAaAx";
85 LIBC_NAMESPACE::swab(from
, to
,
86 LIBC_NAMESPACE::internal::string_length(from
));
87 ASSERT_STREQ(to
, "AaAaAa");