diff --git a/Tests/LibC/CMakeLists.txt b/Tests/LibC/CMakeLists.txt index daec9dae63..ca4570358d 100644 --- a/Tests/LibC/CMakeLists.txt +++ b/Tests/LibC/CMakeLists.txt @@ -15,6 +15,7 @@ set(TEST_SOURCES TestStackSmash.cpp TestStrlcpy.cpp TestStrtodAccuracy.cpp + TestWctype.cpp ) set_source_files_properties(TestStrtodAccuracy.cpp PROPERTIES COMPILE_FLAGS "-fno-builtin-strtod") diff --git a/Tests/LibC/TestWctype.cpp b/Tests/LibC/TestWctype.cpp new file mode 100644 index 0000000000..190d152f6d --- /dev/null +++ b/Tests/LibC/TestWctype.cpp @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021, the SerenityOS developers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +#include + +TEST_CASE(wctype) +{ + // Test that existing properties return non-zero wctypes. + EXPECT(wctype("alnum") != 0); + EXPECT(wctype("alpha") != 0); + EXPECT(wctype("blank") != 0); + EXPECT(wctype("cntrl") != 0); + EXPECT(wctype("digit") != 0); + EXPECT(wctype("graph") != 0); + EXPECT(wctype("lower") != 0); + EXPECT(wctype("print") != 0); + EXPECT(wctype("punct") != 0); + EXPECT(wctype("space") != 0); + EXPECT(wctype("upper") != 0); + EXPECT(wctype("xdigit") != 0); + + // Test that invalid properties return the "invalid" wctype (0). + EXPECT(wctype("") == 0); + EXPECT(wctype("abc") == 0); +} diff --git a/Userland/Libraries/LibC/wctype.cpp b/Userland/Libraries/LibC/wctype.cpp index b3c4acbff6..10830c0c00 100644 --- a/Userland/Libraries/LibC/wctype.cpp +++ b/Userland/Libraries/LibC/wctype.cpp @@ -5,8 +5,25 @@ */ #include +#include #include +enum { + WCTYPE_INVALID = 0, + WCTYPE_ALNUM, + WCTYPE_ALPHA, + WCTYPE_BLANK, + WCTYPE_CNTRL, + WCTYPE_DIGIT, + WCTYPE_GRAPH, + WCTYPE_LOWER, + WCTYPE_PRINT, + WCTYPE_PUNCT, + WCTYPE_SPACE, + WCTYPE_UPPER, + WCTYPE_XDIGIT, +}; + extern "C" { int iswalnum(wint_t wc) @@ -75,10 +92,45 @@ int iswctype(wint_t, wctype_t) TODO(); } -wctype_t wctype(const char*) +wctype_t wctype(const char* property) { - dbgln("FIXME: Implement wctype()"); - TODO(); + if (strcmp(property, "alnum") == 0) + return WCTYPE_ALNUM; + + if (strcmp(property, "alpha") == 0) + return WCTYPE_ALPHA; + + if (strcmp(property, "blank") == 0) + return WCTYPE_BLANK; + + if (strcmp(property, "cntrl") == 0) + return WCTYPE_CNTRL; + + if (strcmp(property, "digit") == 0) + return WCTYPE_DIGIT; + + if (strcmp(property, "graph") == 0) + return WCTYPE_GRAPH; + + if (strcmp(property, "lower") == 0) + return WCTYPE_LOWER; + + if (strcmp(property, "print") == 0) + return WCTYPE_PRINT; + + if (strcmp(property, "punct") == 0) + return WCTYPE_PUNCT; + + if (strcmp(property, "space") == 0) + return WCTYPE_SPACE; + + if (strcmp(property, "upper") == 0) + return WCTYPE_UPPER; + + if (strcmp(property, "xdigit") == 0) + return WCTYPE_XDIGIT; + + return WCTYPE_INVALID; } wint_t towlower(wint_t wc)