Value.format()

This commit is contained in:
Michael Davidsaver
2020-03-18 09:01:47 -07:00
parent 6bba99fce1
commit 1953fbacba
7 changed files with 427 additions and 148 deletions
+191 -58
View File
@@ -100,7 +100,7 @@ void testTypeDef()
testShow()<<def;
testEq(std::string(SB()<<def),
testStrEq(std::string(SB()<<def),
"struct \"simple_t\" {\n"
" double[] value\n"
" struct \"time_t\" {\n"
@@ -128,60 +128,59 @@ void testTypeDef()
auto val = def.create();
testOk1(!!val.valid());
testEq(std::string(SB()<<"\n"<<Value::Helper::desc(val)),
R"out(
[0] struct simple_t parent=[0] [0:11)
achoice -> 10 [10]
any -> 7 [7]
anya -> 8 [8]
arbitrary -> 5 [5]
arbitrary.sarr -> 6 [6]
choice -> 9 [9]
timeStamp -> 2 [2]
timeStamp.nanoseconds -> 4 [4]
timeStamp.secondsPastEpoch -> 3 [3]
value -> 1 [1]
value : 1 [1]
timeStamp : 2 [2]
arbitrary : 5 [5]
any : 7 [7]
anya : 8 [8]
choice : 9 [9]
achoice : 10 [10]
[1] double[] parent=[0] [1:2)
[2] struct time_t parent=[0] [2:5)
nanoseconds -> 2 [4]
secondsPastEpoch -> 1 [3]
secondsPastEpoch : 1 [3]
nanoseconds : 2 [4]
[3] uint64_t parent=[2] [3:4)
[4] uint32_t parent=[2] [4:5)
[5] struct parent=[0] [5:7)
sarr -> 1 [6]
sarr : 1 [6]
[6] struct[] parent=[5] [6:7)
[0] struct parent=[0] [0:2)
value -> 1 [1]
value : 1 [1]
[1] double parent=[0] [1:2)
[7] any parent=[0] [7:8)
[8] any[] parent=[0] [8:9)
[9] union parent=[0] [9:10)
a -> 0 [0]
b -> 1 [1]
a : 0 [0]
[0] float parent=[0] [0:1)
b : 1 [1]
[0] string parent=[0] [0:1)
[10] union[] parent=[0] [10:11)
[0] union parent=[0] [0:1)
x -> 0 [0]
y -> 1 [1]
x : 0 [0]
[0] float parent=[0] [0:1)
y : 1 [1]
[0] float parent=[0] [0:1)
)out")<<"Actual:\n"<<Value::Helper::desc(val);
testStrEq(std::string(SB()<<Value::Helper::desc(val)),
"[0] struct simple_t parent=[0] [0:11)\n"
" achoice -> 10 [10]\n"
" any -> 7 [7]\n"
" anya -> 8 [8]\n"
" arbitrary -> 5 [5]\n"
" arbitrary.sarr -> 6 [6]\n"
" choice -> 9 [9]\n"
" timeStamp -> 2 [2]\n"
" timeStamp.nanoseconds -> 4 [4]\n"
" timeStamp.secondsPastEpoch -> 3 [3]\n"
" value -> 1 [1]\n"
" value : 1 [1]\n"
" timeStamp : 2 [2]\n"
" arbitrary : 5 [5]\n"
" any : 7 [7]\n"
" anya : 8 [8]\n"
" choice : 9 [9]\n"
" achoice : 10 [10]\n"
"[1] double[] parent=[0] [1:2)\n"
"[2] struct time_t parent=[0] [2:5)\n"
" nanoseconds -> 2 [4]\n"
" secondsPastEpoch -> 1 [3]\n"
" secondsPastEpoch : 1 [3]\n"
" nanoseconds : 2 [4]\n"
"[3] uint64_t parent=[2] [3:4)\n"
"[4] uint32_t parent=[2] [4:5)\n"
"[5] struct parent=[0] [5:7)\n"
" sarr -> 1 [6]\n"
" sarr : 1 [6]\n"
"[6] struct[] parent=[5] [6:7)\n"
" [0] struct parent=[0] [0:2)\n"
" value -> 1 [1]\n"
" value : 1 [1]\n"
" [1] double parent=[0] [1:2)\n"
"[7] any parent=[0] [7:8)\n"
"[8] any[] parent=[0] [8:9)\n"
"[9] union parent=[0] [9:10)\n"
" a -> 0 [0]\n"
" b -> 1 [1]\n"
" a : 0 [0]\n"
" [0] float parent=[0] [0:1)\n"
" b : 1 [1]\n"
" [0] string parent=[0] [0:1)\n"
"[10] union[] parent=[0] [10:11)\n"
" [0] union parent=[0] [0:1)\n"
" x -> 0 [0]\n"
" y -> 1 [1]\n"
" x : 0 [0]\n"
" [0] float parent=[0] [0:1)\n"
" y : 1 [1]\n"
" [0] float parent=[0] [0:1)\n"
);
// try to access all field Kinds
@@ -254,7 +253,7 @@ R"out(
testEq(fld["[1].q"].as<std::string>(), "theq");
}
testEq(std::string(SB()<<val),
testStrEq(std::string(SB()<<val),
"struct \"simple_t\" {\n"
" double[] value = {2}[1, 2]\n"
" struct \"time_t\" {\n"
@@ -286,17 +285,151 @@ R"out(
" union.y float = 5\n"
" null\n"
" ]\n"
"}\n")<<"Actual:\n"<<val;
"}\n");
}
//! Returns the frankenstruct
Value neckBolt()
{
using namespace pvxs::members;
return TypeDef(TypeCode::Struct, "top_t", {
Struct("scalar", {
Int32("i32"),
UInt32("u32"),
Bool("b"),
Float64("f64"),
String("s"),
Any("wildcard"),
Union("choice", {
Int32("one"),
Struct("two", {
Int32("ahalf"),
}),
}),
}),
Struct("array", {
Int32A("i32"),
StringA("s"),
AnyA("wildcard"),
UnionA("choice", {
Int32("one"),
Struct("two", {
Int32("ahalf"),
}),
}),
StructA("more", {
Int32("one"),
Struct("two", {
Int32("ahalf"),
}),
}),
}),
}).create();
}
void testFormat()
{
testDiag("%s()", __func__);
Value top(neckBolt());
top["scalar.i32"] = -42;
top["scalar.u32"] = 42;
top["scalar.b"] = true;
top["scalar.f64"] = 123.5;
top["scalar.s"] = "a \"test\"";
top["scalar.wildcard"] = "simple";
top["scalar.choice->one"] = 1024;
top["array.i32"] = shared_array<int32_t>({1,-1,2,-3}).freeze().castTo<const void>();
top["array.s"] = shared_array<std::string>({"one", "two", "three"}).freeze().castTo<const void>();
{
auto fld = top["array.wildcard"];
shared_array<Value> arr(2);
auto temp = arr[0] = TypeDef(TypeCode::String).create();
// arr[1] left null
temp = "simple";
fld = arr.freeze().castTo<const void>();
}
{
auto fld = top["array.choice"];
shared_array<Value> arr(3);
(arr[0] = fld.allocMember())["->one"] = 1357;
// arr[1] left null
(arr[2] = fld.allocMember())["->two.ahalf"] = 2468;
fld = arr.freeze().castTo<const void>();
}
testStrEq(std::string(SB()<<top.format()),
"struct \"top_t\" {\n"
" struct {\n"
" int32_t i32 = -42\n"
" uint32_t u32 = 42\n"
" bool b = true\n"
" double f64 = 123.5\n"
" string s = \"a \\\"test\\\"\"\n"
" any wildcard string = \"simple\"\n"
" union choice.one int32_t = 1024\n"
" } scalar\n"
" struct {\n"
" int32_t[] i32 = {4}[1, -1, 2, -3]\n"
" string[] s = {3}[one, two, three]\n"
" any[] wildcard [\n"
" string = \"simple\"\n"
" null\n"
" ]\n"
" union[] choice [\n"
" union.one int32_t = 1357\n"
" null\n"
" union.two struct {\n"
" int32_t ahalf = 2468\n"
" }\n"
" ]\n"
" struct[] more = [null]\n"
" } array\n"
"}\n"
);
top.mark();
testStrEq(std::string(SB()<<top.format().delta()),
"struct \"top_t\"\n"
"scalar struct\n"
"scalar.i32 int32_t = -42\n"
"scalar.u32 uint32_t = 42\n"
"scalar.b bool = true\n"
"scalar.f64 double = 123.5\n"
"scalar.s string = \"a \\\"test\\\"\"\n"
"scalar.wildcard any\n"
"scalar.wildcard-> string = \"simple\"\n"
"scalar.choice union\n"
"scalar.choice->one int32_t = 1024\n"
"array struct\n"
"array.i32 int32_t[] = {4}[1, -1, 2, -3]\n"
"array.s string[] = {3}[one, two, three]\n"
"array.wildcard any[]\n"
"array.wildcard[0] string = \"simple\"\n"
"array.wildcard[1] null\n"
"array.choice union[]\n"
"array.choice[0] union\n"
"array.choice[0]->one int32_t = 1357\n"
"array.choice[1] null\n"
"array.choice[2] union\n"
"array.choice[2]->two.ahalf int32_t = 2468\n"
"array.more struct[] = [null]\n"
);
}
} // namespace
MAIN(testtype)
{
testPlan(23);
testPlan(25);
showSize();
testBasic();
testTypeDef();
testFormat();
cleanup_for_valgrind();
return testDone();
}