aboutsummaryrefslogtreecommitdiff
path: root/src/system_wrappers/source/data_log_unittest.cc
blob: c64ed94d6c9278899aa6b0ebb1d543a3538ae7f4 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <map>
#include <string>

#include "system_wrappers/interface/data_log.h"
#include "system_wrappers/interface/data_log_c.h"
#include "system_wrappers/source/data_log_c_helpers_unittest.h"
#include "gtest/gtest.h"

using ::webrtc::DataLog;

// A class for storing the values expected from a log table column when
// verifying a log table file.
struct ExpectedValues {
 public:
  ExpectedValues()
    : values(NULL),
      multi_value_length(1) {
  }

  ExpectedValues(std::vector<std::string> expected_values,
                 int expected_multi_value_length)
    : values(expected_values),
      multi_value_length(expected_multi_value_length) {
  }

  std::vector<std::string> values;
  int multi_value_length;
};

typedef std::map<std::string, ExpectedValues> ExpectedValuesMap;

// A static class used for parsing and verifying data log files.
class DataLogParser {
 public:
  // Verifies that the log table stored in the file "log_file" corresponds to
  // the cells and columns specified in "columns".
  static int VerifyTable(FILE* log_file, const ExpectedValuesMap& columns) {
    int row = 0;
    char line_buffer[kMaxLineLength];
    char* ret = fgets(line_buffer, kMaxLineLength, log_file);
    EXPECT_FALSE(ret == NULL);
    if (ret == NULL)
      return -1;

    std::string line(line_buffer, kMaxLineLength);
    VerifyHeader(line, columns);
    while (fgets(line_buffer, kMaxLineLength, log_file) != NULL) {
      line = std::string(line_buffer, kMaxLineLength);
      size_t line_position = 0;

      for (ExpectedValuesMap::const_iterator it = columns.begin();
           it != columns.end(); ++it) {
        std::string str = ParseElement(line, &line_position,
                                       it->second.multi_value_length);
        EXPECT_EQ(str, it->second.values[row]);
        if (str != it->second.values[row])
          return -1;
      }
      ++row;
    }
    return 0;
  }

  // Verifies the table header stored in "line" to correspond with the header
  // specified in "columns".
  static int VerifyHeader(const std::string& line,
                          const ExpectedValuesMap& columns) {
    size_t line_position = 0;
    for (ExpectedValuesMap::const_iterator it = columns.begin();
         it != columns.end(); ++it) {
      std::string str = ParseElement(line, &line_position,
                                     it->second.multi_value_length);
      EXPECT_EQ(str, it->first);
      if (str != it->first)
        return -1;
    }
    return 0;
  }

  // Parses out and returns one element from the string "line", which contains
  // one line read from a log table file. An element can either be a column
  // header or a cell of a row.
  static std::string ParseElement(const std::string& line,
                                  size_t* line_position,
                                  int multi_value_length) {
    std::string parsed_cell;
    parsed_cell = "";
    for (int i = 0; i < multi_value_length; ++i) {
      size_t next_separator = line.find(',', *line_position);
      EXPECT_NE(next_separator, std::string::npos);
      if (next_separator == std::string::npos)
        break;
      parsed_cell += line.substr(*line_position,
                                 next_separator - *line_position + 1);
      *line_position = next_separator + 1;
    }
    return parsed_cell;
  }

  // This constant defines the maximum line length the DataLogParser can
  // parse.
  enum { kMaxLineLength = 100 };
};

TEST(TestDataLog, CreateReturnTest) {
  for (int i = 0; i < 10; ++i)
    ASSERT_EQ(DataLog::CreateLog(), 0);
  ASSERT_EQ(DataLog::AddTable(DataLog::Combine("a proper table", 1)), 0);
  for (int i = 0; i < 10; ++i)
    DataLog::ReturnLog();
  ASSERT_LT(DataLog::AddTable(DataLog::Combine("table failure", 1)), 0);
}

TEST(TestDataLog, VerifyCombineMethod) {
  EXPECT_EQ(std::string("a proper table_1"),
            DataLog::Combine("a proper table", 1));
}

TEST(TestDataLog, VerifySingleTable) {
  DataLog::CreateLog();
  DataLog::AddTable(DataLog::Combine("table", 1));
  DataLog::AddColumn(DataLog::Combine("table", 1), "arrival", 1);
  DataLog::AddColumn(DataLog::Combine("table", 1), "timestamp", 1);
  DataLog::AddColumn(DataLog::Combine("table", 1), "size", 5);
  WebRtc_UWord32 sizes[5] = {1400, 1500, 1600, 1700, 1800};
  for (int i = 0; i < 10; ++i) {
    DataLog::InsertCell(DataLog::Combine("table", 1), "arrival",
                        static_cast<double>(i));
    DataLog::InsertCell(DataLog::Combine("table", 1), "timestamp",
                        static_cast<WebRtc_Word64>(4354 + i));
    DataLog::InsertCell(DataLog::Combine("table", 1), "size", sizes, 5);
    DataLog::NextRow(DataLog::Combine("table", 1));
  }
  DataLog::ReturnLog();
  // Verify file
  FILE* table = fopen("table_1.txt", "r");
  ASSERT_FALSE(table == NULL);
  // Read the column names and verify with the expected columns.
  // Note that the columns are written to file in alphabetical order.
  // Data expected from parsing the file
  const int kNumberOfRows = 10;
  std::string string_arrival[kNumberOfRows] = {
    "0,", "1,", "2,", "3,", "4,",
    "5,", "6,", "7,", "8,", "9,"
  };
  std::string string_timestamp[kNumberOfRows] = {
    "4354,", "4355,", "4356,", "4357,",
    "4358,", "4359,", "4360,", "4361,",
    "4362,", "4363,"
  };
  std::string string_sizes = "1400,1500,1600,1700,1800,";
  ExpectedValuesMap expected;
  expected["arrival,"] = ExpectedValues(
                           std::vector<std::string>(string_arrival,
                                                    string_arrival +
                                                    kNumberOfRows),
                           1);
  expected["size[5],,,,,"] = ExpectedValues(
                               std::vector<std::string>(10, string_sizes), 5);
  expected["timestamp,"] = ExpectedValues(
                             std::vector<std::string>(string_timestamp,
                                                      string_timestamp +
                                                      kNumberOfRows),
                             1);
  ASSERT_EQ(DataLogParser::VerifyTable(table, expected), 0);
  fclose(table);
}

TEST(TestDataLog, VerifyMultipleTables) {
  DataLog::CreateLog();
  DataLog::AddTable(DataLog::Combine("table", 2));
  DataLog::AddTable(DataLog::Combine("table", 3));
  DataLog::AddColumn(DataLog::Combine("table", 2), "arrival", 1);
  DataLog::AddColumn(DataLog::Combine("table", 2), "timestamp", 1);
  DataLog::AddColumn(DataLog::Combine("table", 2), "size", 1);
  DataLog::AddTable(DataLog::Combine("table", 4));
  DataLog::AddColumn(DataLog::Combine("table", 3), "timestamp", 1);
  DataLog::AddColumn(DataLog::Combine("table", 3), "arrival", 1);
  DataLog::AddColumn(DataLog::Combine("table", 4), "size", 1);
  for (WebRtc_Word32 i = 0; i < 10; ++i) {
    DataLog::InsertCell(DataLog::Combine("table", 2), "arrival",
                        static_cast<WebRtc_Word32>(i));
    DataLog::InsertCell(DataLog::Combine("table", 2), "timestamp",
                        static_cast<WebRtc_Word32>(4354 + i));
    DataLog::InsertCell(DataLog::Combine("table", 2), "size",
                        static_cast<WebRtc_Word32>(1200 + 10 * i));
    DataLog::InsertCell(DataLog::Combine("table", 3), "timestamp",
                        static_cast<WebRtc_Word32>(4354 + i));
    DataLog::InsertCell(DataLog::Combine("table", 3), "arrival",
                        static_cast<WebRtc_Word32>(i));
    DataLog::InsertCell(DataLog::Combine("table", 4), "size",
                        static_cast<WebRtc_Word32>(1200 + 10 * i));
    DataLog::NextRow(DataLog::Combine("table", 4));
    DataLog::NextRow(DataLog::Combine("table", 2));
    DataLog::NextRow(DataLog::Combine("table", 3));
  }
  DataLog::ReturnLog();

  // Data expected from parsing the file
  const int kNumberOfRows = 10;
  std::string string_arrival[kNumberOfRows] = {
    "0,", "1,", "2,", "3,", "4,",
    "5,", "6,", "7,", "8,", "9,"
  };
  std::string string_timestamp[kNumberOfRows] = {
    "4354,", "4355,", "4356,", "4357,",
    "4358,", "4359,", "4360,", "4361,",
    "4362,", "4363,"
  };
  std::string string_size[kNumberOfRows] = {
    "1200,", "1210,", "1220,", "1230,",
    "1240,", "1250,", "1260,", "1270,",
    "1280,", "1290,"
  };

  // Verify table 2
  {
    FILE* table = fopen("table_2.txt", "r");
    ASSERT_FALSE(table == NULL);
    ExpectedValuesMap expected;
    expected["arrival,"] = ExpectedValues(
                             std::vector<std::string>(string_arrival,
                                                      string_arrival +
                                                      kNumberOfRows),
                             1);
    expected["size,"] = ExpectedValues(
                          std::vector<std::string>(string_size,
                                                   string_size + kNumberOfRows),
                          1);
    expected["timestamp,"] = ExpectedValues(
                               std::vector<std::string>(string_timestamp,
                                                        string_timestamp +
                                                        kNumberOfRows),
                               1);
    ASSERT_EQ(DataLogParser::VerifyTable(table, expected), 0);
    fclose(table);
  }

  // Verify table 3
  {
    FILE* table = fopen("table_3.txt", "r");
    ASSERT_FALSE(table == NULL);
    ExpectedValuesMap expected;
    expected["arrival,"] = ExpectedValues(
                             std::vector<std::string>(string_arrival,
                                                      string_arrival +
                                                      kNumberOfRows),
                             1);
    expected["timestamp,"] = ExpectedValues(
                             std::vector<std::string>(string_timestamp,
                                                      string_timestamp +
                                                      kNumberOfRows),
                               1);
    ASSERT_EQ(DataLogParser::VerifyTable(table, expected), 0);
    fclose(table);
  }

  // Verify table 4
  {
    FILE* table = fopen("table_4.txt", "r");
    ASSERT_FALSE(table == NULL);
    ExpectedValuesMap expected;
    expected["size,"] = ExpectedValues(
                          std::vector<std::string>(string_size,
                                                   string_size +
                                                   kNumberOfRows),
                          1);
    ASSERT_EQ(DataLogParser::VerifyTable(table, expected), 0);
    fclose(table);
  }
}

TEST(TestDataLogCWrapper, VerifyCWrapper) {
  // Simply call all C wrapper log functions through the C helper unittests.
  // Main purpose is to make sure that the linkage is correct.

  EXPECT_EQ(0, WebRtcDataLogCHelper_TestCreateLog());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestCombine());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestAddTable());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestAddColumn());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestInsertCell_int());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestInsertArray_int());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestNextRow());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestInsertCell_float());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestInsertArray_float());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestNextRow());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestInsertCell_double());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestInsertArray_double());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestNextRow());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestInsertCell_int32());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestInsertArray_int32());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestNextRow());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestInsertCell_uint32());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestInsertArray_uint32());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestNextRow());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestInsertCell_int64());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestInsertArray_int64());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestNextRow());
  EXPECT_EQ(0, WebRtcDataLogCHelper_TestReturnLog());
}