Software APIs
sw
device
lib
ujson
test_helpers.h
1
// Copyright lowRISC contributors (OpenTitan project).
2
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3
// SPDX-License-Identifier: Apache-2.0
4
5
#ifndef OPENTITAN_SW_DEVICE_LIB_UJSON_TEST_HELPERS_H_
6
#define OPENTITAN_SW_DEVICE_LIB_UJSON_TEST_HELPERS_H_
7
8
#include <string>
9
10
#include "sw/device/lib/base/status.h"
11
#include "sw/device/lib/ujson/ujson.h"
12
13
namespace
test_helpers {
14
class
SourceSink
{
15
public
:
16
SourceSink
() {}
17
SourceSink
(
const
std::string &source) : source_(source) {}
18
19
const
std::string &Sink() {
return
sink_; }
20
21
ujson_t
UJson() {
22
return
ujson_init((
void
*)
this
, &SourceSink::getc, &SourceSink::putbuf);
23
}
24
25
void
Reset() {
26
pos_ = 0;
27
sink_.clear();
28
}
29
30
void
Reset(
const
std::string &source) {
31
Reset();
32
source_ = source;
33
}
34
35
status_t
GetChar() {
36
if
(pos_ < source_.size()) {
37
return
OK_STATUS(
static_cast<
uint8_t
>
(source_[pos_++]));
38
}
else
{
39
return
RESOURCE_EXHAUSTED();
40
}
41
}
42
43
status_t
PutBuf(
const
char
*buf,
size_t
len) {
44
sink_.append(buf, len);
45
return
OK_STATUS();
46
}
47
48
private
:
49
static
status_t
getc(
void
*
self
) {
50
return
static_cast<
SourceSink
*
>
(
self
)->GetChar();
51
}
52
53
static
status_t
putbuf(
void
*
self
,
const
char
*buf,
size_t
len) {
54
return
static_cast<
SourceSink
*
>
(
self
)->PutBuf(buf, len);
55
}
56
57
size_t
pos_ = 0;
58
std::string source_;
59
std::string sink_;
60
};
61
}
// namespace test_helpers
62
#endif
// OPENTITAN_SW_DEVICE_LIB_UJSON_TEST_HELPERS_H_
Return to
OpenTitan Documentation