• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MikkelSchubert / adapterremoval / #36

22 Jul 2024 09:33AM UTC coverage: 87.26% (-12.7%) from 100.0%
#36

push

travis-ci

MikkelSchubert
remove duplicate tests

2185 of 2504 relevant lines covered (87.26%)

16293.15 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.9
/src/json.cpp
1
/*************************************************************************\
2
 * AdapterRemoval - cleaning next-generation sequencing reads            *
3
 *                                                                       *
4
 * Copyright (C) 2015 by Mikkel Schubert - mikkelsch@gmail.com           *
5
 *                                                                       *
6
 * This program is free software: you can redistribute it and/or modify  *
7
 * it under the terms of the GNU General Public License as published by  *
8
 * the Free Software Foundation, either version 3 of the License, or     *
9
 * (at your option) any later version.                                   *
10
 *                                                                       *
11
 * This program is distributed in the hope that it will be useful,       *
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 * GNU General Public License for more details.                          *
15
 *                                                                       *
16
 * You should have received a copy of the GNU General Public License     *
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. *
18
\*************************************************************************/
19
#include "json.hpp"
20
#include "debug.hpp" // for AR_REQUIRE
21
#include <algorithm> // for max, find
22
#include <cmath>     // for isinf, isnan
23
#include <memory>    // for make_shared, __shared_ptr_access, shar...
24
#include <sstream>   // for ostringstream
25
#include <utility>   // for pair
26

27
namespace adapterremoval {
28

29
std::string
30
_escape(const std::string& value)
49✔
31
{
32
  std::ostringstream stream;
49✔
33

34
  stream << '"';
49✔
35

36
  for (char c : value) {
730✔
37
    switch (c) {
178✔
38
      case '"':
2✔
39
        stream << "\\\"";
2✔
40
        break;
41
      case '\\':
2✔
42
        stream << "\\\\";
2✔
43
        break;
44
      case '\b':
×
45
        stream << "\\b";
×
46
        break;
47
      case '\f':
×
48
        stream << "\\f";
×
49
        break;
50
      case '\n':
2✔
51
        stream << "\\n";
2✔
52
        break;
53
      case '\r':
×
54
        stream << "\\r";
×
55
        break;
56
      case '\t':
5✔
57
        stream << "\\t";
5✔
58
        break;
59
      default: {
167✔
60
        if (c <= 0x1F) {
167✔
61
          stream << "\\u" << std::hex << static_cast<int>(c);
×
62
        } else {
63
          stream << c;
178✔
64
        }
65
      }
66
    }
67
  }
68

69
  stream << '"';
49✔
70

71
  return stream.str();
98✔
72
}
49✔
73

74
std::string
75
format_f64(double value)
18✔
76
{
77
  AR_REQUIRE(!std::isinf(value));
18✔
78
  if (std::isnan(value)) {
18✔
79
    return "null";
4✔
80
  }
81

82
  std::ostringstream out;
16✔
83
  out.precision(3);
16✔
84
  out << std::fixed << value;
32✔
85

86
  return out.str();
16✔
87
}
16✔
88

89
////////////////////////////////////////////////////////////////////////////////
90

91
std::string
92
json_value::to_string() const
22✔
93
{
94
  std::ostringstream ss;
22✔
95

96
  write(ss);
22✔
97

98
  return ss.str();
44✔
99
}
22✔
100

101
////////////////////////////////////////////////////////////////////////////////
102

103
json_token::json_token(std::string value)
56✔
104
  : m_value(std::move(value))
112✔
105
{
106
}
4✔
107

108
json_ptr
109
json_token::from_str(const std::string& value)
10✔
110
{
111
  return std::make_shared<json_token>(_escape(value));
40✔
112
}
113

114
json_ptr
115
json_token::from_str_vec(const string_vec& values)
3✔
116
{
117
  string_vec escaped;
3✔
118
  for (const auto& it : values) {
36✔
119
    escaped.push_back(_escape(it));
18✔
120
  }
121

122
  return json_token::from_raw_vec(escaped);
6✔
123
}
3✔
124

125
json_ptr
126
json_token::from_i64(const int64_t value)
13✔
127
{
128
  return std::make_shared<json_token>(std::to_string(value));
52✔
129
}
130

131
json_ptr
132
json_token::from_i64_vec(const counts& values)
3✔
133
{
134
  string_vec strings;
3✔
135
  for (size_t i = 0; i < values.size(); ++i) {
24✔
136
    strings.push_back(std::to_string(values.get(i)));
36✔
137
  }
138

139
  return json_token::from_raw_vec(strings);
6✔
140
}
3✔
141

142
json_ptr
143
json_token::from_u64(const uint64_t value)
4✔
144
{
145
  return std::make_shared<json_token>(std::to_string(value));
16✔
146
}
147

148
json_ptr
149
json_token::from_f64(const double value)
6✔
150
{
151
  return std::make_shared<json_token>(format_f64(value));
24✔
152
}
153

154
json_ptr
155
json_token::from_f64_vec(const rates& values)
4✔
156
{
157
  string_vec strings;
4✔
158
  for (size_t i = 0; i < values.size(); ++i) {
32✔
159
    strings.push_back(format_f64(values.get(i)));
48✔
160
  }
161

162
  return json_token::from_raw_vec(strings);
8✔
163
}
4✔
164

165
json_ptr
166
json_token::from_boolean(const bool value)
5✔
167
{
168
  return std::make_shared<json_token>(value ? "true" : "false");
19✔
169
}
170

171
json_ptr
172
json_token::from_null()
4✔
173
{
174
  return std::make_shared<json_token>("null");
12✔
175
}
176

177
void
178
json_token::write(std::ostream& out, size_t /* indent */) const
32✔
179
{
180
  out << m_value;
32✔
181
}
32✔
182

183
json_ptr
184
json_token::from_raw_vec(const string_vec& values)
10✔
185
{
186
  std::ostringstream ss;
10✔
187
  ss << "[";
10✔
188
  for (size_t i = 0; i < values.size(); ++i) {
74✔
189
    if (i) {
27✔
190
      ss << ", ";
17✔
191
    }
192

193
    ss << values.at(i);
81✔
194
  }
195
  ss << "]";
10✔
196

197
  return std::make_shared<json_token>(ss.str());
60✔
198
}
10✔
199

200
////////////////////////////////////////////////////////////////////////////////
201

202
void
203
json_list::write(std::ostream& out, size_t indent_) const
5✔
204
{
205
  const auto indent = std::string(indent_, ' ');
10✔
206

207
  if (m_values.empty()) {
10✔
208
    out << "[]";
2✔
209
  } else {
210
    out << "[\n";
3✔
211

212
    for (size_t i = 0; i < m_values.size(); ++i) {
14✔
213
      out << indent << "  ";
8✔
214
      m_values.at(i)->write(out, indent_ + 2);
12✔
215

216
      if (i < m_values.size() - 1) {
8✔
217
        out << ",";
1✔
218
      }
219

220
      out << "\n";
4✔
221
    }
222

223
    out << indent << "]";
6✔
224
  }
225
}
5✔
226

227
json_dict_ptr
228
json_list::dict()
4✔
229
{
230
  auto ptr = std::make_shared<json_dict>();
4✔
231
  m_values.push_back(ptr);
16✔
232

233
  return ptr;
4✔
234
}
×
235

236
////////////////////////////////////////////////////////////////////////////////
237

238
void
239
json_dict::write(std::ostream& out, size_t indent_) const
32✔
240
{
241
  AR_REQUIRE(m_keys.size() == m_values.size());
96✔
242
  const auto indent = std::string(m_multi_line ? indent_ : 0, ' ');
64✔
243
  const char spacer = m_multi_line ? '\n' : ' ';
32✔
244

245
  if (m_keys.empty()) {
64✔
246
    out << "{}";
5✔
247
  } else {
248
    out << "{" << spacer;
27✔
249

250
    for (size_t i = 0; i < m_keys.size(); ++i) {
120✔
251
      const auto it = m_values.find(m_keys.at(i));
66✔
252
      AR_REQUIRE(it != m_values.end());
99✔
253

254
      out << indent << (m_multi_line ? "  " : "") << _escape(it->first) << ": ";
173✔
255
      it->second->write(out, indent_ + 2);
99✔
256
      if (i < m_keys.size() - 1) {
66✔
257
        out << ",";
6✔
258
      }
259

260
      out << spacer;
33✔
261
    }
262

263
    out << indent << "}";
54✔
264
  }
265
}
32✔
266

267
json_dict_ptr
268
json_dict::dict(const std::string& key)
9✔
269
{
270
  auto ptr = std::make_shared<json_dict>();
9✔
271
  ptr->m_multi_line = m_multi_line;
18✔
272
  _set(key, ptr);
27✔
273

274
  return ptr;
9✔
275
}
×
276

277
json_dict_ptr
278
json_dict::inline_dict(const std::string& key)
6✔
279
{
280
  auto ptr = dict(key);
6✔
281
  ptr->m_multi_line = false;
12✔
282

283
  return ptr;
6✔
284
}
285

286
json_list_ptr
287
json_dict::list(const std::string& key)
2✔
288
{
289
  auto ptr = std::make_shared<json_list>();
2✔
290
  _set(key, ptr);
6✔
291

292
  return ptr;
2✔
293
}
×
294

295
void
296
json_dict::str(const std::string& key, const std::string& value)
6✔
297
{
298
  _set(key, json_token::from_str(value));
12✔
299
}
6✔
300

301
void
302
json_dict::str_vec(const std::string& key, const string_vec& values)
1✔
303
{
304
  _set(key, json_token::from_str_vec(values));
2✔
305
}
1✔
306

307
void
308
json_dict::i64(const std::string& key, const int64_t value)
7✔
309
{
310
  _set(key, json_token::from_i64(value));
14✔
311
}
7✔
312

313
void
314
json_dict::i64_vec(const std::string& key, const counts& values)
1✔
315
{
316
  _set(key, json_token::from_i64_vec(values));
2✔
317
}
1✔
318

319
void
320
json_dict::u64(const std::string& key, const uint64_t value)
×
321
{
322
  _set(key, json_token::from_u64(value));
×
323
}
324

325
void
326
json_dict::f64(const std::string& key, const double value)
2✔
327
{
328
  _set(key, json_token::from_f64(value));
4✔
329
}
2✔
330

331
void
332
json_dict::f64_vec(const std::string& key, const rates& values)
1✔
333
{
334
  _set(key, json_token::from_f64_vec(values));
2✔
335
}
1✔
336

337
void
338
json_dict::boolean(const std::string& key, const bool value)
2✔
339
{
340
  _set(key, json_token::from_boolean(value));
4✔
341
}
2✔
342

343
void
344
json_dict::null(const std::string& key)
2✔
345
{
346
  _set(key, json_token::from_null());
6✔
347
}
2✔
348

349
void
350
json_dict::_set(const std::string& key, const json_ptr& ptr)
33✔
351
{
352
  const auto it = std::find(m_keys.begin(), m_keys.end(), key);
99✔
353
  if (it == m_keys.end()) {
99✔
354
    m_keys.push_back(key);
33✔
355
  }
356

357
  m_values[key] = ptr;
66✔
358
}
33✔
359

360
} // namespace adapterremoval
21✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc