summaryrefslogtreecommitdiff
path: root/src/util/tests/u_printf_test.cpp
blob: fe689958fa2edc043fa711fdf99940bb0e4fe345 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * Copyright © 2022 Yonggang Luo
 *
 * 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 (including the next
 * paragraph) 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.
 */

#include <gtest/gtest.h>

#include "util/memstream.h"
#include "util/u_memory.h"
#include "util/u_printf.h"

class u_printf_test : public ::testing::Test
{
private:
   template<typename T>
   void add_to_buffer(const T& t)
   {
      const uint8_t *data = reinterpret_cast<const uint8_t*>(&t);
      buffer.insert(buffer.end(), data, data + sizeof(t));
   }

protected:
   char *out_buffer = nullptr;
   size_t buffer_size = 0;

   struct format {
      std::vector<char> fmt;
      std::vector<unsigned> args;
   };

   std::vector<format> formats;
   std::vector<char> buffer;
   unsigned last_format;

   virtual void SetUp()
   {
   }

   virtual void TearDown()
   {
      if (out_buffer != NULL) {
         free(out_buffer);
      }
   }

   void add_format(const char *string, std::vector<unsigned> arg_sizes)
   {
      format fmt;
      fmt.fmt = std::vector<char>(string, string + strlen(string) + 1);
      fmt.args = arg_sizes;
      formats.push_back(fmt);
   }

   void use_format(uint32_t idx)
   {
      last_format = idx;
      idx += 1;
      add_to_buffer(idx);
   }

   void add_arg(const char *str)
   {
      format &fmt = formats[last_format];
      uint64_t pos = fmt.fmt.size();

      fmt.fmt.insert(fmt.fmt.end(), str, str + strlen(str) + 1);

      add_to_buffer(pos);
   }

   template<typename T>
   void add_arg(typename std::enable_if<std::is_scalar<T>::value, T>::type t)
   {
      add_to_buffer(t);
   }

   template<typename T, size_t N>
   void add_arg(const T (&t)[N])
   {
      for (unsigned i = 0; i < N; i++)
         add_to_buffer(t[i]);
   }

   std::string parse()
   {
      u_memstream stream;
      FILE* out;
      u_memstream_open(&stream, &out_buffer, &buffer_size);
      out = u_memstream_get(&stream);
      std::vector<u_printf_info> infos;

      for (auto& format : formats) {
         u_printf_info info;
         info.num_args = static_cast<unsigned>(format.args.size());
         info.arg_sizes = format.args.data();
         info.string_size = format.fmt.size();
         info.strings = const_cast<char*>(format.fmt.data());
         infos.push_back(info);
      }

      u_printf(out, buffer.data(), buffer.size(), infos.data(), infos.size());
      u_memstream_close(&stream);

      return out_buffer;
   }
};

TEST_F(u_printf_test, util_printf_next_spec_pos)
{
   EXPECT_EQ(util_printf_next_spec_pos("%d%d", 0), 1);
   EXPECT_EQ(util_printf_next_spec_pos("%%d", 0), -1);
   EXPECT_EQ(util_printf_next_spec_pos("%dd", 0), 1);
   EXPECT_EQ(util_printf_next_spec_pos("%%%%%%", 0), -1);
   EXPECT_EQ(util_printf_next_spec_pos("%%%%%%%d", 0), 7);
   EXPECT_EQ(util_printf_next_spec_pos("abcdef%d", 0), 7);
   EXPECT_EQ(util_printf_next_spec_pos("abcdef%d", 6), 7);
   EXPECT_EQ(util_printf_next_spec_pos("abcdef%d", 7), -1);
   EXPECT_EQ(util_printf_next_spec_pos("abcdef%%", 7), -1);
   EXPECT_EQ(util_printf_next_spec_pos("abcdef%d%d", 0), 7);
   EXPECT_EQ(util_printf_next_spec_pos("%rrrrr%d%d", 0), 7);
   EXPECT_EQ(util_printf_next_spec_pos("%%rrrr%d%d", 0), 7);
}

TEST_F(u_printf_test, util_printf_simple)
{
   add_format("Hello", { });
   use_format(0);
   EXPECT_EQ(parse(), "Hello");
}

TEST_F(u_printf_test, util_printf_single_string)
{
   add_format("%s\n", { 8 });

   use_format(0);
   add_arg("Hello");

   EXPECT_EQ(parse(), "Hello\n");
}

TEST_F(u_printf_test, util_printf_multiple_string)
{
   add_format("foo %s bar\n", { 8 });
   add_format("%s[%s]: %s\n", { 8, 8, 8 });
   add_format("!!! %s !!!", { 8 });

   use_format(2);
   add_arg("ERROR");

   use_format(0);
   add_arg("OpenCL\n");

   use_format(1);
   add_arg("mesa");
   add_arg("12345");
   add_arg("error");

   use_format(0);
   add_arg("ABCDE\n");

   EXPECT_EQ(parse(), "!!! ERROR !!!foo OpenCL\n bar\nmesa[12345]: error\nfoo ABCDE\n bar\n");
}

TEST_F(u_printf_test, util_printf_mixed)
{
   add_format("LOG[%s] %u + %u = %u", { 8, 4, 4, 4 });

   use_format(0);
   add_arg("DEBUG");
   add_arg<uint32_t>(5);
   add_arg<uint32_t>(8);
   add_arg<uint32_t>(13);

   EXPECT_EQ(parse(), "LOG[DEBUG] 5 + 8 = 13");
}

TEST_F(u_printf_test, util_printf_mixed_multiple)
{
   add_format("%%%%%s %u %f %.1v4hlf ", { 8, 4, 4, 16 });
   add_format("%i %u\n", { 4, 4 });
   add_format("ABCED\n", {});
   add_format("foo %hx bar\n", { 2 });

   use_format(2);

   use_format(1);
   add_arg<int32_t>(-5);
   add_arg<uint32_t>(6);

   float floats[4] = {
      1.0,
      2.0,
      3.0,
      4.0
   };
   use_format(0);
   add_arg("mesa");
   add_arg<uint32_t>(10);
   add_arg<float>(3.0);
   add_arg<float, 4>(floats);

   EXPECT_EQ(parse(), "ABCED\n-5 6\n%%%%mesa 10 3.000000 1.0,2.0,3.0,4.0 ");
}