-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathTests.h
More file actions
783 lines (652 loc) · 28.8 KB
/
Tests.h
File metadata and controls
783 lines (652 loc) · 28.8 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/*
* Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved.
*/
#ifndef UNITTESTBOT_TESTS_H
#define UNITTESTBOT_TESTS_H
#include "Include.h"
#include "LineInfo.h"
#include "types/Types.h"
#include "utils/CollectionUtils.h"
#include "utils/PrinterUtils.h"
#include <klee/KTest.h>
#include <klee/TestCase.h>
#include "json.hpp"
#include <tsl/ordered_map.h>
#include <tsl/ordered_set.h>
#include <iterator>
#include <memory>
#include <utility>
#include <queue>
#include <optional>
namespace tests {
using std::string;
using std::vector;
using std::shared_ptr;
const std::string LAZYNAME = "unnamed";
using MapAddressName = std::unordered_map<size_t , std::string>;
bool isUnnamed(char *name);
struct UTBotKTestObject {
std::string name;
std::vector<char> bytes;
std::vector<Offset> offsets;
size_t address;
bool is_lazy = false;
UTBotKTestObject(std::string name,
std::vector<char> bytes,
std::vector<Offset> offsets,
size_t address,
bool is_lazy);
explicit UTBotKTestObject(const ConcretizedObject &kTestObject);
};
struct UTBotKTest {
enum class Status {
SUCCESS,
FAILED
};
vector<UTBotKTestObject> objects;
Status status;
UTBotKTest(std::vector<UTBotKTestObject> objects, Status status)
: objects(std::move(objects)), status(status) {
}
};
using UTBotKTestList = vector<UTBotKTest>;
/**
* This function checks if value string representation is a floating-point special value.
* @param value - string value representation
* @return whether value is a floating-point special value
*/
bool isFPSpecialValue(const string &value);
/**
* This function transforms floating-point special values to C++ representation.
* @param value - string value representation
* @return string value representation in C++
*/
string processFPSpecialValue(const string &value);
/**
* Abstract representation of some value.
*/
struct AbstractValueView {
AbstractValueView() = default;
~AbstractValueView() = default;
public:
/**
* Returns string representation of the value.
*/
[[nodiscard]] virtual string getEntryValue() const = 0;
virtual bool containsFPSpecialValue() {
return false;
}
/**
* Returns subviews of this view.
*/
[[nodiscard]] virtual const vector<shared_ptr<AbstractValueView>> &getSubViews() const {
return this->subViews;
};
/**
* For StructValueView should return vector of string representations of its field values.
*/
virtual vector<string> fieldEntryValues() {
return {};
}
protected:
explicit AbstractValueView(vector<shared_ptr<AbstractValueView>> subViews) : subViews(std::move(subViews)) {}
vector<shared_ptr<AbstractValueView>> subViews{};
};
/**
* It's value is stored as a string. Subviews are always empty.
*/
struct JustValueView : AbstractValueView {
explicit JustValueView(string value) : AbstractValueView(), entryValue(std::move(value)) {}
[[nodiscard]] string getEntryValue() const override {
return entryValue;
}
bool containsFPSpecialValue() override {
return tests::isFPSpecialValue(entryValue);
}
private:
string entryValue;
};
/**
* Value of a void type. Subviews are always empty, entry value is empty.
*/
struct VoidValueView : AbstractValueView {
explicit VoidValueView() = default;
[[nodiscard]] string getEntryValue() const override {
return "";
}
bool containsFPSpecialValue() override {
return false;
}
};
/**
* Representation of primitive value.
*/
struct PrimitiveValueView : JustValueView {
explicit PrimitiveValueView(string value) : JustValueView(std::move(value)) {}
};
/**
* Representation of string value.
*/
struct StringValueView : JustValueView {
public:
explicit StringValueView(string value) : JustValueView(std::move(value)) {}
};
/**
* Representation of pointer to function.
*/
struct FunctionPointerView : JustValueView {
public:
explicit FunctionPointerView(string value) : JustValueView(std::move(value)) {}
};
/**
* Representation of enum.
*/
struct EnumValueView : JustValueView {
public:
explicit EnumValueView(string value) : JustValueView(std::move(value)) {}
};
/**
* Representation of array value. It's value is stored as a string. Subviews of the ArrayValueView are its elements.
*/
struct ArrayValueView : AbstractValueView {
explicit ArrayValueView(vector<shared_ptr<AbstractValueView>> &subViews)
: AbstractValueView(subViews) {}
[[nodiscard]] string getEntryValue() const override {
vector<string> entries;
for (const auto &subView : subViews) {
entries.push_back(subView->getEntryValue());
}
return "{" + StringUtils::joinWith(entries, ", ") + "}";
}
bool containsFPSpecialValue() override {
for (const auto &subView : subViews) {
if (subView->containsFPSpecialValue()) {
return true;
}
}
return false;
}
};
/**
* Representation of struct value. It's value is stored as a string. Subviews of StructValueView are its fields.
* In order to get fields and subfields values (leaves in terms of trees) method fieldEntryValues().
*/
struct StructValueView : AbstractValueView {
explicit StructValueView(vector<shared_ptr<AbstractValueView>> subViews,
std::optional<std::string> entryValue)
: AbstractValueView(std::move(subViews)), entryValue(std::move(entryValue)) {
}
[[nodiscard]] const vector<shared_ptr<AbstractValueView>> &getSubViews() const override {
return this->subViews;
}
[[nodiscard]] string getEntryValue() const override {
if (entryValue.has_value()) {
return entryValue.value();
}
vector<string> entries;
for (const auto &subView : subViews) {
entries.push_back(subView->getEntryValue());
}
return "{" + StringUtils::joinWith(entries, ", ") + "}";
}
vector<string> fieldEntryValues() override {
vector<string> result;
for (const auto &subView : subViews) {
vector<string> subFieldEntryValues = subView->fieldEntryValues();
CollectionUtils::extend(result, subFieldEntryValues);
if (subFieldEntryValues.empty()) {
result.push_back(subView->getEntryValue());
}
}
return result;
}
bool containsFPSpecialValue() override {
for (const auto &subView : subViews) {
if (subView->containsFPSpecialValue()) {
return true;
}
}
return false;
}
private:
std::optional<std::string> entryValue;
};
/**
* Representation of union.
*/
struct UnionValueView : AbstractValueView {
public:
explicit UnionValueView(const string &typeName,
const shared_ptr<AbstractValueView> &rawDataView,
vector<shared_ptr<AbstractValueView>,
std::allocator<shared_ptr<AbstractValueView>>> subViews);
[[nodiscard]] string getEntryValue() const override {
return entryValue;
}
bool containsFPSpecialValue() override {
return false;
}
vector<string> fieldEntryValues() override {
return { getEntryValue() };
}
private:
std::string entryValue;
};
struct InitReference {
std::string varName;
std::string refName;
InitReference(std::string varName, std::string refName)
: varName(std::move(varName)), refName(std::move(refName)) {
}
};
struct Tests {
struct TypeAndVarName {
types::Type type;
string varName;
TypeAndVarName(types::Type type, std::string varName)
: type(std::move(type)), varName(std::move(varName)) {
}
};
struct MethodParam {
types::Type type;
string name;
std::optional<uint64_t> alignment;
bool isPointerToIncomplete = false;
MethodParam(types::Type type,
string name,
std::optional<uint64_t> alignment,
bool isPointerToIncomplete_ = false)
: type(std::move(type)), name(std::move(name)), alignment(std::move(alignment)),
isPointerToIncomplete(isPointerToIncomplete_) {
}
string underscoredName() const {
return "_" + name;
}
bool isChangeable() const {
if((type.isObjectPointer() || type.isLValueReference()) &&
!type.isTypeContainsFunctionPointer() &&
!type.isConstQualifiedValue()) {
return true;
}
return false;
}
std::string dataVariableName() const {
return this->type.isTwoDimensionalPointer() ?
this->underscoredName() :
this->name;
}
};
struct TestCaseParamValue {
string name;
std::optional<uint64_t> alignment;
shared_ptr<AbstractValueView> view;
TestCaseParamValue() = default;
TestCaseParamValue(string name,
std::optional<uint64_t> alignment,
shared_ptr<AbstractValueView> view)
: name(std::move(name)), alignment(alignment), view(std::move(view)) {};
};
struct TestCaseDescription {
string suiteName;
vector<TestCaseParamValue> globalPreValues;
vector<TestCaseParamValue> globalPostValues;
vector<UTBotKTestObject> objects;
vector<MethodParam> stubValuesTypes;
vector<TestCaseParamValue> stubValues;
MapAddressName fromAddressToName;
vector<InitReference> lazyReferences;
vector<TestCaseParamValue> funcParamValues;
vector<TestCaseParamValue> paramPostValues;
TestCaseParamValue returnValue;
TestCaseParamValue functionReturnNotNullValue;
TestCaseParamValue kleePathFlagSymbolicValue;
std::optional <TestCaseParamValue> stdinValue = std::nullopt;
std::optional<TestCaseParamValue> classPreValues;
std::optional<TestCaseParamValue> classPostValues;
};
struct MethodTestCase {
string suiteName;
vector<TestCaseParamValue> globalPreValues;
vector<TestCaseParamValue> globalPostValues;
std::optional <TestCaseParamValue> stdinValue;
vector<TypeAndVarName> lazyVariables;
vector<InitReference> lazyReferences;
vector<UTBotKTestObject> objects;
MapAddressName fromAddressToName;
vector<MethodParam> stubValuesTypes;
vector<TestCaseParamValue> stubValues;
vector<TestCaseParamValue> paramValues;
vector<TestCaseParamValue> paramPostValues;
vector<TestCaseParamValue> lazyValues;
vector<TestCaseParamValue> stubParamValues;
vector<MethodParam> stubParamTypes;
shared_ptr<AbstractValueView> returnValueView;
std::optional<TestCaseParamValue> classPreValues;
std::optional<TestCaseParamValue> classPostValues;
bool isError() const;
};
struct Modifiers {
bool isStatic;
bool isExtern;
bool isInline;
};
struct MethodDescription {
std::optional<MethodParam> classObj;
std::string name;
typedef std::unordered_map<string, string> SuiteNameToCodeTextMap;
std::string stubsText;
SuiteNameToCodeTextMap codeText;
std::string paramsString;
types::Type returnType;
bool hasPointerToIncompleteReturnType = false;
std::optional<string> sourceBody;
Modifiers modifiers;
bool isVariadic = false;
std::vector<MethodParam> globalParams;
std::vector<MethodParam> params;
typedef std::unordered_map<string, std::shared_ptr<types::FunctionInfo>> FPointerMap;
FPointerMap functionPointers;
vector<MethodTestCase> testCases;
typedef std::unordered_map<string, vector<MethodTestCase>> SuiteNameToTestCasesMap;
SuiteNameToTestCasesMap suiteTestCases;
bool operator==(const MethodDescription &other) const;
MethodDescription();
[[nodiscard]] vector<types::Type> getParamTypes() const {
return CollectionUtils::transform(params, [](auto const& param) {
return param.type;
});
}
[[nodiscard]] vector<string> getParamNames() const {
return CollectionUtils::transform(params, [](MethodParam const& param) {
return param.name;
});
}
[[nodiscard]] types::FunctionInfo toFunctionInfo() {
types::FunctionInfo fInfo;
fInfo.isArray = false;
fInfo.name = name;
fInfo.returnType = returnType;
for (const auto& param: params) {
fInfo.params.push_back({param.type, param.name});
}
return fInfo;
}
[[nodiscard]] static MethodDescription fromFunctionInfo(const types::FunctionInfo& fInfo) {
MethodDescription method;
method.name = fInfo.name;
method.returnType = fInfo.returnType;
for (const auto& param: fInfo.params) {
method.params.emplace_back(param.type, param.name, std::nullopt);
}
return method;
}
bool hasChangeable() const {
for(const auto& i : params) {
if (i.isChangeable()) {
return true;
}
}
return false;
}
bool isClassMethod() const {
return classObj.has_value();
}
std::optional<std::string> getClassName() const {
if (isClassMethod()) {
return std::make_optional(classObj->name);
}
return std::nullopt;
}
std::optional<std::string> getClassTypeName() const {
if (isClassMethod()) {
return std::make_optional(classObj->type.typeName());
}
return std::nullopt;
}
};
struct MethodDescriptionToStringEqual {
using is_transparent [[maybe_unused]] = void;
};
struct MethodDescriptionHash {
std::size_t operator()(const MethodDescription &methodDescription) const;
};
using MethodsMap = tsl::ordered_map<std::string, MethodDescription>;
static const string DEFAULT_SUITE_NAME;
static const string ERROR_SUITE_NAME;
static const MethodParam &getStdinMethodParam();
fs::path sourceFilePath;
string sourceFileNameNoExt; // without extension
fs::path relativeFileDir; // relative to project root dir
string testFilename;
fs::path testHeaderFilePath;
fs::path testSourceFilePath;
vector<Include> srcFileHeaders;
vector<Include> headersBeforeMainHeader;
std::optional<Include> mainHeader;
MethodsMap methods; // method's name -> description
string code; // contains final code of test file
string headerCode; // contains code of header
std::vector<string> commentBlocks{};
string stubs; // language-independent stubs definitions
std::uint32_t errorMethodsNumber;
std::uint32_t regressionMethodsNumber;
bool isFilePresentedInCommands = true;
bool isFilePresentedInArtifact = true;
};
typedef CollectionUtils::OrderedMapFileTo<Tests> TestsMap;
struct TestMethod {
std::string methodName;
fs::path bitcodeFilePath;
fs::path sourceFilePath;
bool operator==(const TestMethod &rhs) const;
bool operator!=(const TestMethod &rhs) const;
TestMethod(string methodName, fs::path bitcodeFile, fs::path sourceFilename);
};
using MethodKtests = std::unordered_map<TestMethod, UTBotKTestList, HashUtils::TestMethodHash>;
/**
* This class provides functionality to parse Klee output given in
* KTestObject format.
*/
class KTestObjectParser {
public:
explicit KTestObjectParser(types::TypesHandler &typesHandler)
: typesHandler(typesHandler){};
/**
* Parses given klee objects, reads result ot the testsMap.
* @param batch
* @param tests
* @param filterByLineFlag
* @param predicateInfo
* @param verbose
*/
void parseKTest(const MethodKtests &batch,
tests::Tests &tests,
const std::unordered_map<string, types::Type>& methodNameToReturnTypeMap,
bool filterByLineFlag,
shared_ptr<LineInfo> lineInfo);
private:
types::TypesHandler &typesHandler;
struct RawKleeParam {
string paramName;
vector<char> rawData;
RawKleeParam(string paramName, vector<char> rawData)
: paramName(std::move(paramName)), rawData(std::move(rawData)) {
}
[[nodiscard]] [[maybe_unused]] bool hasPrefix(const string &prefix) const {
return StringUtils::startsWith(paramName, prefix);
}
};
struct JsonNumAndType {
int num;
types::Type type;
};
/**
* Parses KTestObject that represents test cases generated by Klee.
* @param cases
* @param filterByLineFlag
* @param predicateInfo
* @param methodDescription
*/
void parseTestCases(const UTBotKTestList &cases,
bool filterByLineFlag,
Tests::MethodDescription &methodDescription,
const std::unordered_map<string, types::Type>& methodNameToReturnTypeMap,
shared_ptr<LineInfo> lineInfo);
/**
* Parses parameters that are stored in given objects. Then parameters
* are written into paramValues.
* @param testCases
* @param filterByLineFlag
* @param predicateInfo
* @param methodDescription
* @param traceStream
*/
Tests::TestCaseDescription
parseTestCaseParameters(const UTBotKTest &testCases,
Tests::MethodDescription &methodDescription,
const std::unordered_map<string, types::Type>& methodNameToReturnTypeMap,
std::stringstream &traceStream);
shared_ptr<AbstractValueView>
testParameterView(const RawKleeParam &kleeParam,
const Tests::TypeAndVarName ¶m,
types::PointerUsage usage,
const MapAddressName &fromAddressToName,
std::vector<InitReference> &initReferences,
const std::optional<const Tests::MethodDescription> &testingMethod = std::nullopt);
shared_ptr<ArrayValueView> multiArrayView(const vector<char> &byteArray,
const types::Type &type,
size_t arraySize,
size_t offset,
types::PointerUsage usage);
shared_ptr<ArrayValueView> arrayView(const vector<char> &byteArray,
const types::Type &type,
size_t arraySize,
unsigned int offset,
types::PointerUsage usage);
static shared_ptr<StringValueView> stringLiteralView(const vector<char> &byteArray,
size_t length = 0);
shared_ptr<FunctionPointerView> functionPointerView(const std::optional<string>& scopeName,
const string &methodName,
const string ¶mName);
shared_ptr<FunctionPointerView> functionPointerView(const string &structName,
const string &fieldName);
shared_ptr<UnionValueView> unionView(const vector<char> &byteArray,
types::UnionInfo &unionInfo,
unsigned int offset,
types::PointerUsage usage);
shared_ptr<StructValueView> structView(const vector<char> &byteArray,
types::StructInfo &curStruct,
unsigned int offset,
types::PointerUsage usage);
shared_ptr<StructValueView> structView(const vector<char> &byteArray,
types::StructInfo &curStruct,
unsigned int offset,
types::PointerUsage usage,
const std::optional<const Tests::MethodDescription> &testingMethod,
const std::string &name,
const MapAddressName &fromAddressToName,
std::vector<InitReference> &initReferences);
shared_ptr<PrimitiveValueView> primitiveView(const vector<char> &byteArray,
const types::Type &type,
size_t offset,
size_t len);
static shared_ptr<EnumValueView> enumView(const vector<char> &byteArray,
types::EnumInfo &enumInfo,
size_t offset,
size_t len);
string primitiveCharView(const types::Type &type, string value);
static string primitiveBoolView(const string &value);
constexpr static const char *const KLEE_PATH_FLAG = "kleePathFlag";
const std::string PointerWidthType = "unsigned long long";
const size_t PointerWidthSize = 8;
constexpr static const char *const KLEE_PATH_FLAG_SYMBOLIC = "kleePathFlagSymbolic";
static vector<RawKleeParam>::const_iterator
getKleeParam(const vector<RawKleeParam> &rawKleeParams, std::string name);
static RawKleeParam getKleeParamOrThrow(const vector<RawKleeParam> &rawKleeParams, const std::string &name);
Tests::TestCaseDescription
parseTestCaseParams(const UTBotKTest &ktest,
const Tests::MethodDescription &methodDescription,
const std::unordered_map<string, types::Type>& methodNameToReturnTypeMap,
const std::stringstream &traceStream);
vector<shared_ptr<AbstractValueView>> collectUnionSubViews(const vector<char> &byteArray,
const types::UnionInfo &info,
unsigned int offset,
types::PointerUsage usage);
void processGlobalParamPreValue(Tests::TestCaseDescription &testCaseDescription,
const Tests::MethodParam &globalParam,
vector<RawKleeParam> &rawKleeParams);
void processSymbolicStdin(Tests::TestCaseDescription &testCaseDescription,
vector<RawKleeParam> &rawKleeParams);
void processGlobalParamPostValue(Tests::TestCaseDescription &testCaseDescription,
const Tests::MethodParam &globalParam,
vector<RawKleeParam> &rawKleeParams);
void processClassPostValue(Tests::TestCaseDescription &testCaseDescription,
const Tests::MethodParam ¶m,
vector<RawKleeParam> &rawKleeParams);
void processParamPostValue(Tests::TestCaseDescription &testCaseDescription,
const Tests::MethodParam ¶m,
vector<RawKleeParam> &rawKleeParams);
void processStubParamValue(Tests::TestCaseDescription &testCaseDescription,
const std::unordered_map<string, types::Type>& methodNameToReturnTypeMap,
vector<RawKleeParam> &rawKleeParams);
void assignTypeUnnamedVar(Tests::MethodTestCase &testCase,
const Tests::MethodDescription &methodDescription);
void assignTypeStubVar(Tests::MethodTestCase &testCase,
const Tests::MethodDescription &methodDescription);
void workWithStructInBFS(std::queue<JsonNumAndType> &order, std::vector<bool> &visited,
const Offset &off, std::vector<UTBotKTestObject> &objects, const types::StructInfo &structInfo);
int findFieldIndex(const types::StructInfo &structInfo, size_t offset);
int findObjectIndex(const std::vector<UTBotKTestObject> &objects, const std::string &name);
types::Type traverseStruct(const types::StructInfo &structInfo, size_t offset);
};
/**
* @brief This function is used for converting primiive value of a specific type
* To a string value which we can print to .cpp file.
*/
template <typename T>
std::enable_if_t<!std::is_floating_point<T>::value, string>
primitiveValueToString(T value) {
return std::to_string(value);
}
template <typename T>
std::enable_if_t<std::is_floating_point<T>::value, string>
primitiveValueToString(T value) {
std::stringstream ss;
ss << std::scientific;
ss << value;
return ss.str();
}
/**
* This function is used for converting sequence of bytes to specific type.
* Returns string representation of a value recorded in byteArray.
* @tparam T - type of value
* @param byteArray
* @param offset - initial position of a value in byteArray
* @param len - size of T
* @return string representation of value
*/
template <typename T>
string readBytesAsValue(const vector<char> &byteArray, size_t offset, size_t len) {
char bytes[len];
for (int j = 0; j < len; j++) {
bytes[j] = byteArray[offset + j];
}
T *pTypeValue = (T *)bytes;
T pValue = *pTypeValue;
return primitiveValueToString<T>(pValue);
}
/**
* Same as readBytesAsValue, but returns result of readBytesAsValue that is already
* parametrized by type that corresponds to given typeName.
* @param typeName - string name of type
* @param byteArray - array of bytes
* @param offset - initial position
* @param len - size of type that corresponds to typeName
* @return string representation of value.
*/
string readBytesAsValueForType(const vector<char> &byteArray,
const string &typeName,
unsigned int offset,
unsigned int len);
}
#endif // UNITTESTBOT_TESTS_H