Blender  V3.3
obj_import_string_utils_tests.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0 */
2 
4 
5 #include "testing/testing.h"
6 
7 namespace blender::io::obj {
8 
9 #define EXPECT_STRREF_EQ(str1, str2) EXPECT_STREQ(str1, std::string(str2).c_str())
10 
11 TEST(obj_import_string_utils, read_next_line)
12 {
13  std::string str = "abc\n \n\nline with \t spaces\nCRLF ending:\r\na";
14  StringRef s = str;
18  EXPECT_STRREF_EQ("line with \t spaces", read_next_line(s));
19  EXPECT_STRREF_EQ("CRLF ending:\r", read_next_line(s));
21  EXPECT_TRUE(s.is_empty());
22 }
23 
24 TEST(obj_import_string_utils, fixup_line_continuations)
25 {
26  const char *str =
27  "backslash \\\n eol\n"
28  "backslash spaces \\ \n eol\n"
29  "without eol \\ is \\\\ \\ left intact\n"
30  "\\";
31  const char *exp =
32  "backslash eol\n"
33  "backslash spaces eol\n"
34  "without eol \\ is \\\\ \\ left intact\n"
35  "\\";
36  std::string buf(str);
37  fixup_line_continuations(buf.data(), buf.data() + buf.size());
38  EXPECT_STRREF_EQ(exp, buf);
39 }
40 
42 {
43  return StringRef(drop_whitespace(s.begin(), s.end()), s.end());
44 }
45 static StringRef parse_int(StringRef s, int fallback, int &dst, bool skip_space = true)
46 {
47  return StringRef(parse_int(s.begin(), s.end(), fallback, dst, skip_space), s.end());
48 }
50  float fallback,
51  float &dst,
52  bool skip_space = true,
53  bool require_trailing_space = false)
54 {
55  return StringRef(
56  parse_float(s.begin(), s.end(), fallback, dst, skip_space, require_trailing_space), s.end());
57 }
58 
59 TEST(obj_import_string_utils, drop_whitespace)
60 {
61  /* Empty */
63  /* Only whitespace */
66  EXPECT_STRREF_EQ("", drop_whitespace(" \t\n\r "));
67  /* Drops leading whitespace */
69  EXPECT_STRREF_EQ("a b", drop_whitespace(" a b"));
70  EXPECT_STRREF_EQ("a b ", drop_whitespace(" a b "));
71  /* No leading whitespace */
73  /* Case with backslash, should be treated as whitespace */
74  EXPECT_STRREF_EQ("d", drop_whitespace(" \t d"));
75 }
76 
77 TEST(obj_import_string_utils, parse_int_valid)
78 {
79  std::string str = "1 -10 \t 1234 1234567890 +7 123a";
80  StringRef s = str;
81  int val;
82  s = parse_int(s, 0, val);
83  EXPECT_EQ(1, val);
84  s = parse_int(s, 0, val);
85  EXPECT_EQ(-10, val);
86  s = parse_int(s, 0, val);
87  EXPECT_EQ(1234, val);
88  s = parse_int(s, 0, val);
89  EXPECT_EQ(1234567890, val);
90  s = parse_int(s, 0, val);
91  EXPECT_EQ(7, val);
92  s = parse_int(s, 0, val);
93  EXPECT_EQ(123, val);
94  EXPECT_STRREF_EQ("a", s);
95 }
96 
97 TEST(obj_import_string_utils, parse_int_invalid)
98 {
99  int val;
100  /* Invalid syntax */
101  EXPECT_STRREF_EQ("--123", parse_int("--123", -1, val));
102  EXPECT_EQ(val, -1);
103  EXPECT_STRREF_EQ("foobar", parse_int("foobar", -2, val));
104  EXPECT_EQ(val, -2);
105  /* Out of integer range */
106  EXPECT_STRREF_EQ(" a", parse_int("1234567890123 a", -3, val));
107  EXPECT_EQ(val, -3);
108  /* Has leading white-space when we don't expect it */
109  EXPECT_STRREF_EQ(" 1", parse_int(" 1", -4, val, false));
110  EXPECT_EQ(val, -4);
111 }
112 
113 TEST(obj_import_string_utils, parse_float_valid)
114 {
115  std::string str = "1 -10 123.5 -17.125 0.1 1e6 50.0e-1";
116  StringRef s = str;
117  float val;
118  s = parse_float(s, 0, val);
119  EXPECT_EQ(1.0f, val);
120  s = parse_float(s, 0, val);
121  EXPECT_EQ(-10.0f, val);
122  s = parse_float(s, 0, val);
123  EXPECT_EQ(123.5f, val);
124  s = parse_float(s, 0, val);
125  EXPECT_EQ(-17.125f, val);
126  s = parse_float(s, 0, val);
127  EXPECT_EQ(0.1f, val);
128  s = parse_float(s, 0, val);
129  EXPECT_EQ(1.0e6f, val);
130  s = parse_float(s, 0, val);
131  EXPECT_EQ(5.0f, val);
132  EXPECT_TRUE(s.is_empty());
133 }
134 
135 TEST(obj_import_string_utils, parse_float_invalid)
136 {
137  float val;
138  /* Invalid syntax */
139  EXPECT_STRREF_EQ("_0", parse_float("_0", -1.0f, val));
140  EXPECT_EQ(val, -1.0f);
141  EXPECT_STRREF_EQ("..5", parse_float("..5", -2.0f, val));
142  EXPECT_EQ(val, -2.0f);
143  /* Out of float range. Current float parser (fast_float)
144  * clamps out of range numbers to +/- infinity, so this
145  * one gets a +inf instead of fallback -3.0. */
146  EXPECT_STRREF_EQ(" a", parse_float("9.0e500 a", -3.0f, val));
147  EXPECT_EQ(val, std::numeric_limits<float>::infinity());
148  /* Has leading white-space when we don't expect it */
149  EXPECT_STRREF_EQ(" 1", parse_float(" 1", -4.0f, val, false));
150  EXPECT_EQ(val, -4.0f);
151  /* Has trailing non-number characters when we don't want them */
152  EXPECT_STRREF_EQ("123.5.png", parse_float(" 123.5.png", -5.0f, val, true, true));
153  EXPECT_EQ(val, -5.0f);
154 }
155 
156 } // namespace blender::io::obj
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
constexpr const char * begin() const
constexpr const char * end() const
constexpr bool is_empty() const
#define str(s)
ccl_device_inline float3 exp(float3 v)
Definition: math_float3.h:392
void fixup_line_continuations(char *p, char *end)
const char * parse_int(const char *p, const char *end, int fallback, int &dst, bool skip_space)
TEST(obj_exporter_utils, append_negative_frame_to_filename)
const char * drop_whitespace(const char *p, const char *end)
StringRef read_next_line(StringRef &buffer)
const char * parse_float(const char *p, const char *end, float fallback, float &dst, bool skip_space, bool require_trailing_space)
#define EXPECT_STRREF_EQ(str1, str2)