diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Android.mk | 1 | ||||
-rw-r--r-- | tests/ctype_test.cpp | 138 | ||||
-rw-r--r-- | tests/stdio_test.cpp | 59 | ||||
-rw-r--r-- | tests/wchar_test.cpp | 100 |
4 files changed, 284 insertions, 14 deletions
diff --git a/tests/Android.mk b/tests/Android.mk index 32037a6..d2bcce8 100644 --- a/tests/Android.mk +++ b/tests/Android.mk @@ -40,6 +40,7 @@ test_cflags = \ libBionicStandardTests_src_files := \ buffer_tests.cpp \ + ctype_test.cpp \ dirent_test.cpp \ eventfd_test.cpp \ fcntl_test.cpp \ diff --git a/tests/ctype_test.cpp b/tests/ctype_test.cpp new file mode 100644 index 0000000..7b27d64 --- /dev/null +++ b/tests/ctype_test.cpp @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +#include <ctype.h> + +TEST(ctype, isalnum) { + EXPECT_TRUE(isalnum('1')); + EXPECT_TRUE(isalnum('a')); + EXPECT_TRUE(isalnum('A')); + EXPECT_FALSE(isalnum('!')); + EXPECT_FALSE(isalnum(' ')); +} + +TEST(ctype, isalpha) { + EXPECT_FALSE(isalpha('1')); + EXPECT_TRUE(isalpha('a')); + EXPECT_TRUE(isalpha('A')); + EXPECT_FALSE(isalpha('!')); + EXPECT_FALSE(isalpha(' ')); +} + +TEST(ctype, isascii) { + EXPECT_TRUE(isascii('\x7f')); + EXPECT_FALSE(isascii('\x80')); +} + +TEST(ctype, isblank) { + EXPECT_FALSE(isblank('1')); + EXPECT_TRUE(isblank(' ')); + EXPECT_TRUE(isblank('\t')); +} + +TEST(ctype, iscntrl) { + EXPECT_FALSE(iscntrl('1')); + EXPECT_TRUE(iscntrl('\b')); +} + +TEST(ctype, isdigit) { + EXPECT_TRUE(isdigit('1')); + EXPECT_FALSE(isdigit('a')); + EXPECT_FALSE(isdigit('x')); +} + +TEST(ctype, isgraph) { + EXPECT_TRUE(isgraph('a')); + EXPECT_TRUE(isgraph('A')); + EXPECT_TRUE(isgraph('1')); + EXPECT_TRUE(isgraph('!')); + EXPECT_FALSE(isgraph(' ')); +} + +TEST(ctype, islower) { + EXPECT_TRUE(islower('a')); + EXPECT_FALSE(islower('A')); + EXPECT_FALSE(islower('!')); +} + +TEST(ctype, isprint) { + EXPECT_TRUE(isprint('a')); + EXPECT_TRUE(isprint(' ')); + EXPECT_FALSE(isprint('\b')); +} + +TEST(ctype, ispunct) { + EXPECT_TRUE(ispunct('!')); + EXPECT_FALSE(ispunct('a')); + EXPECT_FALSE(ispunct(' ')); + EXPECT_FALSE(ispunct('\b')); +} + +TEST(ctype, isspace) { + EXPECT_TRUE(isspace(' ')); + EXPECT_TRUE(isspace('\f')); + EXPECT_TRUE(isspace('\n')); + EXPECT_TRUE(isspace('\r')); + EXPECT_TRUE(isspace('\t')); + EXPECT_TRUE(isspace('\v')); + EXPECT_FALSE(isspace('a')); + EXPECT_FALSE(isspace('!')); +} + +TEST(ctype, isupper) { + EXPECT_TRUE(isupper('A')); + EXPECT_FALSE(isupper('a')); + EXPECT_FALSE(isupper('!')); +} + +TEST(ctype, isxdigit) { + EXPECT_TRUE(isxdigit('0')); + EXPECT_FALSE(isxdigit('x')); + EXPECT_TRUE(isxdigit('1')); + EXPECT_TRUE(isxdigit('a')); + EXPECT_TRUE(isxdigit('A')); + EXPECT_FALSE(isxdigit('g')); + EXPECT_FALSE(isxdigit(' ')); +} + +TEST(ctype, toascii) { + EXPECT_EQ('a', toascii('a')); + EXPECT_EQ('a', toascii(0x80 | 'a')); +} + +TEST(ctype, tolower) { + EXPECT_EQ('!', tolower('!')); + EXPECT_EQ('a', tolower('a')); + EXPECT_EQ('a', tolower('A')); +} + +TEST(ctype, _tolower) { + // _tolower may mangle characters for which isupper is false. + EXPECT_EQ('a', _tolower('A')); +} + +TEST(ctype, toupper) { + EXPECT_EQ('!', toupper('!')); + EXPECT_EQ('A', toupper('a')); + EXPECT_EQ('A', toupper('A')); +} + +TEST(ctype, _toupper) { + // _toupper may mangle characters for which islower is false. + EXPECT_EQ('A', _toupper('a')); +} diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp index 0aa1d15..d825c14 100644 --- a/tests/stdio_test.cpp +++ b/tests/stdio_test.cpp @@ -23,6 +23,7 @@ #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> +#include <wchar.h> TEST(stdio, tmpfile_fileno_fprintf_rewind_fgets) { FILE* fp = tmpfile(); @@ -192,18 +193,38 @@ TEST(stdio, printf_ssize_t) { snprintf(buf, sizeof(buf), "%zd", v); } -TEST(stdio, snprintf_n_format_specifier_not_implemented) { -#if defined(__BIONIC__) +// https://code.google.com/p/android/issues/detail?id=64886 +TEST(stdio, snprintf_a) { + char buf[BUFSIZ]; + EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235)); + EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf); +} + +TEST(stdio, snprintf_lc) { + char buf[BUFSIZ]; + wint_t wc = L'a'; + EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc)); + EXPECT_STREQ("<a>", buf); +} + +TEST(stdio, snprintf_ls) { + char buf[BUFSIZ]; + wchar_t* ws = NULL; + EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws)); + EXPECT_STREQ("<(null)>", buf); + + wchar_t chars[] = { L'h', L'i', 0 }; + ws = chars; + EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws)); + EXPECT_STREQ("<hi>", buf); +} + +TEST(stdio, snprintf_n) { char buf[32]; int i = 0; - // We deliberately don't implement %n, so it's treated like - // any other unrecognized format specifier. - EXPECT_EQ(5, snprintf(buf, sizeof(buf), "a %n b", &i)); - EXPECT_EQ(0, i); - EXPECT_STREQ("a n b", buf); -#else // __BIONIC__ - GTEST_LOG_(INFO) << "This test does nothing.\n"; -#endif // __BIONIC__ + EXPECT_EQ(4, snprintf(buf, sizeof(buf), "a %n b", &i)); + EXPECT_EQ(2, i); + EXPECT_STREQ("a b", buf); } TEST(stdio, snprintf_smoke) { @@ -306,19 +327,19 @@ TEST(stdio, snprintf_smoke) { TEST(stdio, snprintf_f_special) { char buf[BUFSIZ]; snprintf(buf, sizeof(buf), "%f", nanf("")); - EXPECT_STREQ("NaN", buf); + EXPECT_STRCASEEQ("NaN", buf); snprintf(buf, sizeof(buf), "%f", HUGE_VALF); - EXPECT_STREQ("Inf", buf); + EXPECT_STRCASEEQ("Inf", buf); } TEST(stdio, snprintf_g_special) { char buf[BUFSIZ]; snprintf(buf, sizeof(buf), "%g", nan("")); - EXPECT_STREQ("NaN", buf); + EXPECT_STRCASEEQ("NaN", buf); snprintf(buf, sizeof(buf), "%g", HUGE_VAL); - EXPECT_STREQ("Inf", buf); + EXPECT_STRCASEEQ("Inf", buf); } TEST(stdio, snprintf_d_INT_MAX) { @@ -365,6 +386,16 @@ TEST(stdio, snprintf_lld_LLONG_MIN) { EXPECT_STREQ("-9223372036854775808", buf); } +TEST(stdio, snprintf_e) { + char buf[BUFSIZ]; + + snprintf(buf, sizeof(buf), "%e", 1.5); + EXPECT_STREQ("1.500000e+00", buf); + + snprintf(buf, sizeof(buf), "%Le", 1.5l); + EXPECT_STREQ("1.500000e+00", buf); +} + TEST(stdio, popen) { FILE* fp = popen("cat /proc/version", "r"); ASSERT_TRUE(fp != NULL); diff --git a/tests/wchar_test.cpp b/tests/wchar_test.cpp index 20566f2..d5d27ed 100644 --- a/tests/wchar_test.cpp +++ b/tests/wchar_test.cpp @@ -16,6 +16,7 @@ #include <gtest/gtest.h> +#include <errno.h> #include <limits.h> #include <wchar.h> @@ -55,3 +56,102 @@ TEST(wchar, wctomb_wcrtomb) { EXPECT_EQ(1U, wcrtomb(bytes, L'h', NULL)); EXPECT_EQ('h', bytes[0]); } + +TEST(wchar, wcstombs_wcrtombs) { + const wchar_t chars[] = { L'h', L'e', L'l', L'l', L'o', 0 }; + const wchar_t bad_chars[] = { L'h', L'i', 666, 0 }; + const wchar_t* src; + char bytes[BUFSIZ]; + + // Given a NULL destination, these functions count valid characters. + EXPECT_EQ(5U, wcstombs(NULL, chars, 0)); + EXPECT_EQ(5U, wcstombs(NULL, chars, 4)); + EXPECT_EQ(5U, wcstombs(NULL, chars, 256)); + src = chars; + EXPECT_EQ(5U, wcsrtombs(NULL, &src, 0, NULL)); + EXPECT_EQ(&chars[0], src); + src = chars; + EXPECT_EQ(5U, wcsrtombs(NULL, &src, 4, NULL)); + EXPECT_EQ(&chars[0], src); + src = chars; + EXPECT_EQ(5U, wcsrtombs(NULL, &src, 256, NULL)); + EXPECT_EQ(&chars[0], src); + + // An unrepresentable char just returns an error from wcstombs... + errno = 0; + EXPECT_EQ(static_cast<size_t>(-1), wcstombs(NULL, bad_chars, 0)); + EXPECT_EQ(EILSEQ, errno); + errno = 0; + EXPECT_EQ(static_cast<size_t>(-1), wcstombs(NULL, bad_chars, 256)); + EXPECT_EQ(EILSEQ, errno); + + // And wcsrtombs doesn't tell us where it got stuck because we didn't ask it + // to actually convert anything... + errno = 0; + src = bad_chars; + EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(NULL, &src, 0, NULL)); + EXPECT_EQ(&bad_chars[0], src); + EXPECT_EQ(EILSEQ, errno); + errno = 0; + src = bad_chars; + EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(NULL, &src, 256, NULL)); + EXPECT_EQ(&bad_chars[0], src); + EXPECT_EQ(EILSEQ, errno); + + // Okay, now let's test actually converting something... + memset(bytes, 'x', sizeof(bytes)); + EXPECT_EQ(0U, wcstombs(bytes, chars, 0)); + memset(bytes, 'x', sizeof(bytes)); + EXPECT_EQ(4U, wcstombs(bytes, chars, 4)); + bytes[5] = 0; + EXPECT_STREQ("hellx", bytes); + memset(bytes, 'x', sizeof(bytes)); + EXPECT_EQ(5U, wcstombs(bytes, chars, 256)); + EXPECT_STREQ("hello", bytes); + memset(bytes, 'x', sizeof(bytes)); + EXPECT_EQ(5U, wcstombs(bytes, chars, 6)); + EXPECT_STREQ("hello", bytes); + errno = 0; + memset(bytes, 'x', sizeof(bytes)); + EXPECT_EQ(static_cast<size_t>(-1), wcstombs(bytes, bad_chars, 256)); + EXPECT_EQ(EILSEQ, errno); + bytes[3] = 0; + EXPECT_STREQ("hix", bytes); + + // wcsrtombs is a bit more informative... + memset(bytes, 'x', sizeof(bytes)); + src = chars; + EXPECT_EQ(0U, wcsrtombs(bytes, &src, 0, NULL)); + EXPECT_EQ(&chars[0], src); // No input consumed. + EXPECT_EQ(EILSEQ, errno); + + memset(bytes, 'x', sizeof(bytes)); + src = chars; + EXPECT_EQ(4U, wcsrtombs(bytes, &src, 4, NULL)); + EXPECT_EQ(&chars[4], src); // Some input consumed. + EXPECT_EQ(EILSEQ, errno); + bytes[5] = 0; + EXPECT_STREQ("hellx", bytes); + + memset(bytes, 'x', sizeof(bytes)); + src = chars; + EXPECT_EQ(5U, wcsrtombs(bytes, &src, 256, NULL)); + EXPECT_EQ(NULL, src); // All input consumed! + EXPECT_EQ(EILSEQ, errno); + EXPECT_STREQ("hello", bytes); + + memset(bytes, 'x', sizeof(bytes)); + src = chars; + EXPECT_EQ(5U, wcsrtombs(bytes, &src, 6, NULL)); + EXPECT_EQ(NULL, src); // All input consumed. + EXPECT_EQ(EILSEQ, errno); + EXPECT_STREQ("hello", bytes); + + memset(bytes, 'x', sizeof(bytes)); + src = bad_chars; + EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(bytes, &src, 256, NULL)); + EXPECT_EQ(&bad_chars[2], src); + EXPECT_EQ(EILSEQ, errno); + bytes[3] = 0; + EXPECT_STREQ("hix", bytes); +} |