89 lines · cpp
1//===-- Unittests for wmemset ---------------------------------------------===//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 "hdr/types/size_t.h"10#include "hdr/types/wchar_t.h"11#include "src/wchar/wmemset.h"12#include "test/UnitTest/Test.h"13 14TEST(LlvmLibcWMemsetTest, SmallStringBoundCheck) {15 wchar_t str[5];16 for (int i = 0; i < 5; i++)17 str[i] = 'A';18 19 wchar_t *output = LIBC_NAMESPACE::wmemset(str + 1, 'B', 3);20 21 EXPECT_EQ(output, str + 1);22 23 // EXPECT_TRUE being used since there isn't currently support for printing24 // wide chars in the future, it would be preferred to switch these to25 // EXPECT_EQ26 EXPECT_TRUE(str[0] == (wchar_t)'A');27 EXPECT_TRUE(str[1] == (wchar_t)'B');28 EXPECT_TRUE(str[2] == (wchar_t)'B');29 EXPECT_TRUE(str[3] == (wchar_t)'B');30 EXPECT_TRUE(str[4] == (wchar_t)'A');31}32 33TEST(LlvmLibcWMemsetTest, LargeStringBoundCheck) {34 constexpr int str_size = 1000;35 wchar_t str[str_size];36 for (int i = 0; i < str_size; i++)37 str[i] = 'A';38 39 wchar_t *output = LIBC_NAMESPACE::wmemset(str + 1, 'B', str_size - 2);40 41 EXPECT_EQ(output, str + 1);42 43 EXPECT_TRUE(str[0] == (wchar_t)'A');44 for (int i = 1; i < str_size - 1; i++)45 EXPECT_TRUE(str[i] == (wchar_t)'B');46 47 EXPECT_TRUE(str[str_size - 1] == (wchar_t)'A');48}49 50TEST(LlvmLibcWMemsetTest, WCharSizeSmallString) {51 // ensure we can handle full range of widechars52 wchar_t str[5];53 const wchar_t target = WCHAR_MAX;54 55 for (int i = 0; i < 5; i++)56 str[i] = 'A';57 58 wchar_t *output = LIBC_NAMESPACE::wmemset(str + 1, target, 3);59 60 EXPECT_EQ(output, str + 1);61 62 EXPECT_TRUE(str[0] == (wchar_t)'A');63 EXPECT_TRUE(str[1] == target);64 EXPECT_TRUE(str[2] == target);65 EXPECT_TRUE(str[3] == target);66 EXPECT_TRUE(str[4] == (wchar_t)'A');67}68 69TEST(LlvmLibcWMemsetTest, WCharSizeLargeString) {70 // ensure we can handle full range of widechars71 constexpr int str_size = 1000;72 wchar_t str[str_size];73 74 const wchar_t target = WCHAR_MAX;75 76 for (int i = 0; i < str_size; i++)77 str[i] = 'A';78 79 wchar_t *output = LIBC_NAMESPACE::wmemset(str + 1, target, str_size - 2);80 81 EXPECT_EQ(output, str + 1);82 83 EXPECT_TRUE(str[0] == (wchar_t)'A');84 for (int i = 1; i < str_size - 1; i++)85 EXPECT_TRUE(str[i] == target);86 87 EXPECT_TRUE(str[str_size - 1] == (wchar_t)'A');88}89