blob: b9252684da0486813a1705cd92e65c8616ada809 [file] [log] [blame]
K. Moon832a6942022-10-31 20:11:31 +00001// Copyright 2017 The PDFium Authors
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -04002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Dan Sinclaire0345a42017-10-30 20:20:42 +00005#include "fxjs/cjs_util.h"
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -04006
Lei Zhang3111d402022-04-18 22:24:07 +00007#include <iterator>
8
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -04009#include "testing/gtest/include/gtest/gtest.h"
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -040010
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -040011TEST(CJS_Util, ParseDataType) {
12 struct ParseDataTypeCase {
13 const wchar_t* const input_string;
Tom Sepez86390d72021-05-20 17:11:36 +000014 const CJS_Util::DataType expected;
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -040015 };
16
17 // Commented out tests follow the spec but are not passing.
18 const ParseDataTypeCase cases[] = {
19 // Not conversions
Tom Sepez86390d72021-05-20 17:11:36 +000020 {L"", CJS_Util::DataType::kInvalid},
21 {L"d", CJS_Util::DataType::kInvalid},
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -040022
23 // Simple cases
Tom Sepez86390d72021-05-20 17:11:36 +000024 {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 Nakashima3a4ebcc2017-07-14 14:24:42 -040028
29 // nDecSep Not implemented
Tom Sepez86390d72021-05-20 17:11:36 +000030 // {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 Nakashima3a4ebcc2017-07-14 14:24:42 -040034 // {L"%,4d", -1},
35 // {L"%,d", -1},
36
37 // cFlags("+ 0#"") are only valid for numeric conversions.
Tom Sepez86390d72021-05-20 17:11:36 +000038 {L"%+d", CJS_Util::DataType::kInt},
39 {L"%+x", CJS_Util::DataType::kInt},
40 {L"%+f", CJS_Util::DataType::kDouble},
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -040041 // {L"%+s", -1},
Tom Sepez86390d72021-05-20 17:11:36 +000042 {L"% d", CJS_Util::DataType::kInt},
43 {L"% x", CJS_Util::DataType::kInt},
44 {L"% f", CJS_Util::DataType::kDouble},
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -040045 // {L"% s", -1},
Tom Sepez86390d72021-05-20 17:11:36 +000046 {L"%0d", CJS_Util::DataType::kInt},
47 {L"%0x", CJS_Util::DataType::kInt},
48 {L"%0f", CJS_Util::DataType::kDouble},
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -040049 // {L"%0s", -1},
Tom Sepez86390d72021-05-20 17:11:36 +000050 {L"%#d", CJS_Util::DataType::kInt},
51 {L"%#x", CJS_Util::DataType::kInt},
52 {L"%#f", CJS_Util::DataType::kDouble},
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -040053 // {L"%#s", -1},
54
55 // nWidth should work. for all conversions, can be combined with cFlags=0
56 // for numbers.
Tom Sepez86390d72021-05-20 17:11:36 +000057 {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 Nakashima3a4ebcc2017-07-14 14:24:42 -040064 // {L"%05s", -1},
65
66 // nPrecision should only work for float
67 // {L"%.5d", -1},
68 // {L"%.5x", -1},
Tom Sepez86390d72021-05-20 17:11:36 +000069 {L"%.5f", CJS_Util::DataType::kDouble},
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -040070 // {L"%.5s", -1},
71 // {L"%.14d", -1},
72 // {L"%.14x", -1},
Tom Sepez86390d72021-05-20 17:11:36 +000073 {L"%.14f", CJS_Util::DataType::kDouble},
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -040074 // {L"%.14s", -1},
75 // {L"%.f", -1},
76
Tom Sepezffbc0d92017-07-17 09:29:05 -070077 // See https://crbug.com/740166
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -040078 // nPrecision too large (> 260) causes crashes in Windows.
Tom Sepezffbc0d92017-07-17 09:29:05 -070079 // Avoid this by limiting to two digits
Tom Sepez86390d72021-05-20 17:11:36 +000080 {L"%.1d", CJS_Util::DataType::kInt},
81 {L"%.10d", CJS_Util::DataType::kInt},
82 {L"%.100d", CJS_Util::DataType::kInvalid},
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -040083
84 // Unexpected characters
Tom Sepez86390d72021-05-20 17:11:36 +000085 {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 Nakashima3a4ebcc2017-07-14 14:24:42 -0400101
102 // Combine multiple valid components
Tom Sepez86390d72021-05-20 17:11:36 +0000103 {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 Nakashima3a4ebcc2017-07-14 14:24:42 -0400107 };
108
Lei Zhang3111d402022-04-18 22:24:07 +0000109 for (size_t i = 0; i < std::size(cases); i++) {
Lei Zhang93fd8a82019-12-12 20:29:42 +0000110 WideString input(cases[i].input_string);
Dan Sinclairf7435522018-02-05 22:27:22 +0000111 EXPECT_EQ(cases[i].expected, CJS_Util::ParseDataType(&input))
Henrique Nakashima3a4ebcc2017-07-14 14:24:42 -0400112 << cases[i].input_string;
113 }
114}