Browse Source

LibC: Implement towctrans

Tim Schumacher 3 năm trước cách đây
mục cha
commit
42031f026a
2 tập tin đã thay đổi với 28 bổ sung3 xóa
  1. 17 0
      Tests/LibC/TestWctype.cpp
  2. 11 3
      Userland/Libraries/LibC/wctype.cpp

+ 17 - 0
Tests/LibC/TestWctype.cpp

@@ -66,3 +66,20 @@ TEST_CASE(iswctype)
         EXPECT(iswctype(test_chars[i], -1) == 0);
     }
 }
+
+TEST_CASE(towctrans)
+{
+    const wint_t test_chars[] = { L'A', L'a', L'F', L'f', L'Z', L'z', L'0', L'\n', L'.', L'\x00' };
+
+    // Test that valid mappings are wired to the correct implementation.
+    for (unsigned int i = 0; i < sizeof(test_chars) / sizeof(test_chars[0]); i++) {
+        EXPECT(towctrans(test_chars[i], wctrans("tolower")) == towlower(test_chars[i]));
+        EXPECT(towctrans(test_chars[i], wctrans("toupper")) == towupper(test_chars[i]));
+    }
+
+    // Test that invalid mappings always return the character unchanged.
+    for (unsigned int i = 0; i < sizeof(test_chars) / sizeof(test_chars[0]); i++) {
+        EXPECT(towctrans(test_chars[i], 0) == test_chars[i]);
+        EXPECT(towctrans(test_chars[i], -1) == test_chars[i]);
+    }
+}

+ 11 - 3
Userland/Libraries/LibC/wctype.cpp

@@ -187,10 +187,18 @@ wint_t towupper(wint_t wc)
     return __inline_toupper(wc);
 }
 
-wint_t towctrans(wint_t, wctrans_t)
+wint_t towctrans(wint_t wc, wctrans_t desc)
 {
-    dbgln("FIXME: Implement towctrans()");
-    TODO();
+    switch (desc) {
+    case WCTRANS_TOLOWER:
+        return towlower(wc);
+
+    case WCTRANS_TOUPPER:
+        return towupper(wc);
+
+    default:
+        return wc;
+    }
 }
 
 wctrans_t wctrans(const char* charclass)