K. Moon | 832a694 | 2022-10-31 20:11:31 +0000 | [diff] [blame] | 1 | // Copyright 2017 The PDFium Authors |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Dan Sinclair | e0345a4 | 2017-10-30 20:20:42 +0000 | [diff] [blame] | 5 | #include "fxjs/cjs_util.h" |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 6 | |
Lei Zhang | 3111d40 | 2022-04-18 22:24:07 +0000 | [diff] [blame] | 7 | #include <iterator> |
| 8 | |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 9 | #include "testing/gtest/include/gtest/gtest.h" |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 10 | |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 11 | TEST(CJS_Util, ParseDataType) { |
| 12 | struct ParseDataTypeCase { |
| 13 | const wchar_t* const input_string; |
Tom Sepez | 86390d7 | 2021-05-20 17:11:36 +0000 | [diff] [blame] | 14 | const CJS_Util::DataType expected; |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 15 | }; |
| 16 | |
| 17 | // Commented out tests follow the spec but are not passing. |
| 18 | const ParseDataTypeCase cases[] = { |
| 19 | // Not conversions |
Tom Sepez | 86390d7 | 2021-05-20 17:11:36 +0000 | [diff] [blame] | 20 | {L"", CJS_Util::DataType::kInvalid}, |
| 21 | {L"d", CJS_Util::DataType::kInvalid}, |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 22 | |
| 23 | // Simple cases |
Tom Sepez | 86390d7 | 2021-05-20 17:11:36 +0000 | [diff] [blame] | 24 | {L"%d", CJS_Util::DataType::kInt}, |
| 25 | {L"%x", CJS_Util::DataType::kInt}, |
| 26 | {L"%f", CJS_Util::DataType::kDouble}, |
| 27 | {L"%s", CJS_Util::DataType::kString}, |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 28 | |
| 29 | // nDecSep Not implemented |
Tom Sepez | 86390d7 | 2021-05-20 17:11:36 +0000 | [diff] [blame] | 30 | // {L"%,0d", CJS_Util::DataType::kInt}, |
| 31 | // {L"%,1d", CJS_Util::DataType::kInt}, |
| 32 | // {L"%,2d", CJS_Util::DataType::kInt}, |
| 33 | // {L"%,3d", CJS_Util::DataType::kInt}, |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 34 | // {L"%,4d", -1}, |
| 35 | // {L"%,d", -1}, |
| 36 | |
| 37 | // cFlags("+ 0#"") are only valid for numeric conversions. |
Tom Sepez | 86390d7 | 2021-05-20 17:11:36 +0000 | [diff] [blame] | 38 | {L"%+d", CJS_Util::DataType::kInt}, |
| 39 | {L"%+x", CJS_Util::DataType::kInt}, |
| 40 | {L"%+f", CJS_Util::DataType::kDouble}, |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 41 | // {L"%+s", -1}, |
Tom Sepez | 86390d7 | 2021-05-20 17:11:36 +0000 | [diff] [blame] | 42 | {L"% d", CJS_Util::DataType::kInt}, |
| 43 | {L"% x", CJS_Util::DataType::kInt}, |
| 44 | {L"% f", CJS_Util::DataType::kDouble}, |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 45 | // {L"% s", -1}, |
Tom Sepez | 86390d7 | 2021-05-20 17:11:36 +0000 | [diff] [blame] | 46 | {L"%0d", CJS_Util::DataType::kInt}, |
| 47 | {L"%0x", CJS_Util::DataType::kInt}, |
| 48 | {L"%0f", CJS_Util::DataType::kDouble}, |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 49 | // {L"%0s", -1}, |
Tom Sepez | 86390d7 | 2021-05-20 17:11:36 +0000 | [diff] [blame] | 50 | {L"%#d", CJS_Util::DataType::kInt}, |
| 51 | {L"%#x", CJS_Util::DataType::kInt}, |
| 52 | {L"%#f", CJS_Util::DataType::kDouble}, |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 53 | // {L"%#s", -1}, |
| 54 | |
| 55 | // nWidth should work. for all conversions, can be combined with cFlags=0 |
| 56 | // for numbers. |
Tom Sepez | 86390d7 | 2021-05-20 17:11:36 +0000 | [diff] [blame] | 57 | {L"%5d", CJS_Util::DataType::kInt}, |
| 58 | {L"%05d", CJS_Util::DataType::kInt}, |
| 59 | {L"%5x", CJS_Util::DataType::kInt}, |
| 60 | {L"%05x", CJS_Util::DataType::kInt}, |
| 61 | {L"%5f", CJS_Util::DataType::kDouble}, |
| 62 | {L"%05f", CJS_Util::DataType::kDouble}, |
| 63 | {L"%5s", CJS_Util::DataType::kString}, |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 64 | // {L"%05s", -1}, |
| 65 | |
| 66 | // nPrecision should only work for float |
| 67 | // {L"%.5d", -1}, |
| 68 | // {L"%.5x", -1}, |
Tom Sepez | 86390d7 | 2021-05-20 17:11:36 +0000 | [diff] [blame] | 69 | {L"%.5f", CJS_Util::DataType::kDouble}, |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 70 | // {L"%.5s", -1}, |
| 71 | // {L"%.14d", -1}, |
| 72 | // {L"%.14x", -1}, |
Tom Sepez | 86390d7 | 2021-05-20 17:11:36 +0000 | [diff] [blame] | 73 | {L"%.14f", CJS_Util::DataType::kDouble}, |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 74 | // {L"%.14s", -1}, |
| 75 | // {L"%.f", -1}, |
| 76 | |
Tom Sepez | ffbc0d9 | 2017-07-17 09:29:05 -0700 | [diff] [blame] | 77 | // See https://crbug.com/740166 |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 78 | // nPrecision too large (> 260) causes crashes in Windows. |
Tom Sepez | ffbc0d9 | 2017-07-17 09:29:05 -0700 | [diff] [blame] | 79 | // Avoid this by limiting to two digits |
Tom Sepez | 86390d7 | 2021-05-20 17:11:36 +0000 | [diff] [blame] | 80 | {L"%.1d", CJS_Util::DataType::kInt}, |
| 81 | {L"%.10d", CJS_Util::DataType::kInt}, |
| 82 | {L"%.100d", CJS_Util::DataType::kInvalid}, |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 83 | |
| 84 | // Unexpected characters |
Tom Sepez | 86390d7 | 2021-05-20 17:11:36 +0000 | [diff] [blame] | 85 | {L"%ad", CJS_Util::DataType::kInvalid}, |
| 86 | {L"%bx", CJS_Util::DataType::kInvalid}, |
| 87 | // {L"%cf", CJS_Util::DataType::kInvalid}, |
| 88 | // {L"%es", CJS_Util::DataType::kInvalid}, |
| 89 | // {L"%gd", CJS_Util::DataType::kInvalid}, |
| 90 | {L"%hx", CJS_Util::DataType::kInvalid}, |
| 91 | // {L"%if", CJS_Util::DataType::kInvalid}, |
| 92 | {L"%js", CJS_Util::DataType::kInvalid}, |
| 93 | {L"%@d", CJS_Util::DataType::kInvalid}, |
| 94 | {L"%~x", CJS_Util::DataType::kInvalid}, |
| 95 | {L"%[f", CJS_Util::DataType::kInvalid}, |
| 96 | {L"%\0s", CJS_Util::DataType::kInvalid}, |
| 97 | {L"%\nd", CJS_Util::DataType::kInvalid}, |
| 98 | {L"%\rx", CJS_Util::DataType::kInvalid}, |
| 99 | // {L"%%f", CJS_Util::DataType::kInvalid}, |
| 100 | // {L"% s", CJS_Util::DataType::kInvalid}, |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 101 | |
| 102 | // Combine multiple valid components |
Tom Sepez | 86390d7 | 2021-05-20 17:11:36 +0000 | [diff] [blame] | 103 | {L"%+6d", CJS_Util::DataType::kInt}, |
| 104 | {L"% 7x", CJS_Util::DataType::kInt}, |
| 105 | {L"%#9.3f", CJS_Util::DataType::kDouble}, |
| 106 | {L"%10s", CJS_Util::DataType::kString}, |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 107 | }; |
| 108 | |
Lei Zhang | 3111d40 | 2022-04-18 22:24:07 +0000 | [diff] [blame] | 109 | for (size_t i = 0; i < std::size(cases); i++) { |
Lei Zhang | 93fd8a8 | 2019-12-12 20:29:42 +0000 | [diff] [blame] | 110 | WideString input(cases[i].input_string); |
Dan Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 111 | EXPECT_EQ(cases[i].expected, CJS_Util::ParseDataType(&input)) |
Henrique Nakashima | 3a4ebcc | 2017-07-14 14:24:42 -0400 | [diff] [blame] | 112 | << cases[i].input_string; |
| 113 | } |
| 114 | } |