// MIT License // // Copyright (c) 2016 Alfred Agrell // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. //The above license applies only to the WuTF directory, not the entire Arlib. #if 0 //You don't need this. It's just a bunch of tests for WuTF itself. //To run: Flip the above #if, then // $ g++ *.cpp -std=c++11 && ./a.out // C:\> g++ *.cpp -std=c++11 -lcomdlg32 && a.exe //The tests require C++11, for char16_t. WuTF itself is plain C. #include "wutf.h" #include static bool test816_core(int flags, const char* utf8, const char16_t* utf16_exp, int inlen=0, int outlen_e=0) { int iflags = flags & WUTF_INVALID_MASK; if ((flags&WUTF_WTF8) && iflags==WUTF_INVALID_DCXX) return true; if (!inlen) { while (utf8[inlen]) inlen++; inlen++; } if (!outlen_e) { while (utf16_exp[outlen_e]) outlen_e++; outlen_e++; } uint16_t utf16_act[128]; int outlen_a = WuTF_utf8_to_utf16(flags, utf8, inlen, utf16_act, 128); int outpos_a=0; int outpos_e=0; int outlen_a2 = WuTF_utf8_to_utf16(flags, utf8, inlen, NULL, 0); if (outlen_a != outlen_a2) { printf("Expected length %i, got %i\n", outlen_a, outlen_a2); goto fail; } if (iflags == WUTF_INVALID_ABORT && outlen_a < 0) { for (int i=0;i8 is way less tested than 8->16, and 8<->32 isn't tested at all test168(u"a", "a"); test168(u"smörgåsräka", "smörgåsräka"); test168(u"♩♪♫♬", "♩♪♫♬"); test168(u"𝄞♩♪♫♬", "𝄞♩♪♫♬"); test168(u"#\x0000#", "#\x00#", 4,4); test168(u"#\x0080#", "#\xc2\x80#"); test168(u"#\x0800#", "#\xe0\xa0\x80#"); test168(u"#\xD800\xDC00#", "#\xf0\x90\x80\x80#"); test168(u"#\x007F#", "#\x7f#"); test168(u"#\x07FF#", "#\xdf\xbf#"); test168(u"#\xFFFF#", "#\xef\xbf\xbf#"); test168(u"#\xD7FF#", "#\xed\x9f\xbf#"); test168(u"#\xE000#", "#\xee\x80\x80#"); test168(u"#\xFFFD#", "#\xef\xbf\xbd#"); test168(u"#\xDBFF\xDFFF#", "#\xf4\x8f\xbf\xbf#"); test168f(WUTF_WTF8, u"#\xD800#", "#\xed\xa0\x80#"); test168f(WUTF_WTF8, u"#\xDBFF#", "#\xed\xaf\xbf#"); test168f(WUTF_WTF8, u"#\xDC00#", "#\xed\xb0\x80#"); test168f(WUTF_WTF8, u"#\xDFFF#", "#\xed\xbf\xbf#"); } #ifdef _WIN32 #define SMR "sm\xC3\xB6rg\xC3\xA5sr\xC3\xA4ka" #define SMR_W L"sm\x00F6rg\x00E5sr\x00E4ka" #define SMR3 "\xC3\xA5\xC3\xA4\xC3\xB6" #define SMR3_W L"\x00E5\x00E4\x00F6" #include static void testOFN(); //To pass, //(1) "(1) PASS" must show up in the console. //(2) "(2) PASS" must show up in the console. //(3) The five .åäö files must show up in the filename chooser dialog. //(3) you_shouldnt_see_this.txt must NOT show up. //(3) The instructions (file type field) must be ungarbled. //(3) You must follow the filetype instructions. //(3) "(3) PASS" must show up in the console. //(4) The text on the button must be correct. //(4) The text on the button window's title must be correct. //(5) The message box title must be correct. //(6) The message box title must be correct. //Explanation: //(1) CreateFileA(); a simple thing //(2) GetWindowTextA(); it returns strings, rather than taking them //(3) GetOpenFileNameA(); a complex thing //(4) Test text on a button; also on the window title, but (5) also tests that //(5) Message box title //(6) Message box body, and a string that's longer than the 260 limit //4, 5 and 6 should be verified before dismissing the message box. void WuTF_test_ansiutf() { WuTF_enable(); DWORD ignore; HANDLE h; h = CreateFileW(SMR_W L".txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); WriteFile(h, "pokemon", 8, &ignore, NULL); CloseHandle(h); h = CreateFileA(SMR ".txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); if (h != INVALID_HANDLE_VALUE) { char p[8]; ReadFile(h, p, 42, &ignore, NULL); if (!strcmp(p, "pokemon")) puts("(1) PASS"); else puts("(1) FAIL: Wrong contents"); CloseHandle(h); } else { printf("(1) FAIL: Couldn't open file (errno %lu)", GetLastError()); } DeleteFileW(SMR_W L".txt"); HWND wnd = CreateWindowA("BUTTON", "(4) CHECK: " SMR, WS_OVERLAPPEDWINDOW|WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 200, 60, NULL, NULL, NULL, NULL); WCHAR expect[42]; GetWindowTextW(wnd, expect, 42); if (!wcscmp(expect, L"(4) CHECK: " SMR_W)) puts("(2) PASS"); else puts("(2) PASS"); testOFN(); //this one takes two string arguments, one of which can be way longer than 260 #define PAD "Stretch string to 260 characters." #define PAD2 PAD " " PAD #define PAD8 PAD2 "\r\n" PAD2 "\r\n" PAD2 "\r\n" PAD2 MessageBoxA(NULL, PAD8 "\r\n(6) CHECK: " SMR, "(5) CHECK: " SMR, MB_OK); } static void testOFN() { CreateDirectoryA(SMR, NULL); CloseHandle(CreateFileW(SMR_W L"/" SMR_W L"1." SMR3_W, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)); CloseHandle(CreateFileW(SMR_W L"/" SMR_W L"2." SMR3_W, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)); CloseHandle(CreateFileW(SMR_W L"/" SMR_W L"3." SMR3_W, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)); CloseHandle(CreateFileW(SMR_W L"/" SMR_W L"4." SMR3_W, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)); CloseHandle(CreateFileW(SMR_W L"/" SMR_W L"5." SMR3_W, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)); CloseHandle(CreateFileW(SMR_W L"/you_shouldnt_see_this.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)); OPENFILENAME ofn; char ofnret[65536]; memset(&ofn, 0, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.lpstrFilter = "Select the " SMR3 " files\0*." SMR3 "\0"; ofn.nFilterIndex = 1; ofn.lpstrFile = ofnret; ofn.nMaxFile = 65536; ofn.lpstrInitialDir = SMR; ofn.Flags = OFN_ALLOWMULTISELECT|OFN_EXPLORER; GetOpenFileNameA(&ofn); char* filenames = ofnret; int numcorrect = 0; while (*filenames) { puts(filenames); if (strlen(filenames)==strlen(SMR "?." SMR3)) { filenames[strlen(SMR)]='?'; if (!strcmp(filenames, SMR "?." SMR3)) numcorrect++; else numcorrect = -1000; } filenames += strlen(filenames)+1; } if (numcorrect == 5) { puts("(3) PASS"); } else { puts("(3) FAIL"); } } #endif int main() { WuTF_test_encoder(); #ifdef _WIN32 WuTF_test_ansiutf(); #endif } #endif