|
| 1 | +/*========================================================================= |
| 2 | + * |
| 3 | + * Copyright NumFOCUS |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0.txt |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + *=========================================================================*/ |
| 18 | + |
| 19 | +// Round-trip a small file at a path that contains non-ASCII (UTF-8) |
| 20 | +// characters. On Windows, the Windows-utf8-codepage.manifest attached |
| 21 | +// to every ITK test driver makes the narrow Win32 *A APIs treat byte |
| 22 | +// strings as UTF-8 (Windows 10 1903 and later), so std::ofstream and |
| 23 | +// std::ifstream constructed from a UTF-8 const char * "just work". On |
| 24 | +// POSIX platforms the encoding is byte-transparent. |
| 25 | + |
| 26 | +#include <cstdio> |
| 27 | +#include <fstream> |
| 28 | +#include <string> |
| 29 | + |
| 30 | +#include "gtest/gtest.h" |
| 31 | + |
| 32 | +namespace |
| 33 | +{ |
| 34 | + |
| 35 | +// "speciäl-fil👍.txt" encoded as UTF-8 bytes. |
| 36 | +const char * |
| 37 | +SpecialBasename() |
| 38 | +{ |
| 39 | + return "speci\xC3\xA4l-fil\xF0\x9F\x91\x8D.txt"; |
| 40 | +} |
| 41 | + |
| 42 | +std::string |
| 43 | +TempDirFromEnv() |
| 44 | +{ |
| 45 | + for (const char * var : { "ITK_TEST_OUTPUT_DIR", "TEMP", "TMP", "TMPDIR" }) |
| 46 | + { |
| 47 | + if (const char * envDir = std::getenv(var)) |
| 48 | + { |
| 49 | + return envDir; |
| 50 | + } |
| 51 | + } |
| 52 | + return "."; |
| 53 | +} |
| 54 | + |
| 55 | +} // namespace |
| 56 | + |
| 57 | +TEST(WindowsUtf8Codepage, RoundTripFileAtNonAsciiPath) |
| 58 | +{ |
| 59 | + const std::string path = TempDirFromEnv() + "/" + SpecialBasename(); |
| 60 | + const std::string payload = "round-trip payload\n"; |
| 61 | + |
| 62 | + { |
| 63 | + std::ofstream out(path.c_str()); |
| 64 | + ASSERT_TRUE(out.is_open()) << "failed to open " << path << " for writing"; |
| 65 | + out << payload; |
| 66 | + } |
| 67 | + |
| 68 | + std::ifstream in(path.c_str()); |
| 69 | + const bool readOpened = in.is_open(); |
| 70 | + const std::string actual = |
| 71 | + readOpened ? std::string((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()) : std::string{}; |
| 72 | + std::remove(path.c_str()); |
| 73 | + |
| 74 | + ASSERT_TRUE(readOpened) << "failed to open " << path << " for reading"; |
| 75 | + EXPECT_EQ(actual, payload); |
| 76 | +} |
0 commit comments