2 * SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2024 EfficiOS, Inc.
9 #include "cpp-common/bt2c/c-string-view.hpp"
15 template <typename StrT
>
16 const char *asConstCharPtr(StrT
&& val
)
21 const char *asConstCharPtr(const char * const val
)
26 const char *typeName(bt2c::CStringView
)
28 return "bt2c::CStringView";
31 const char *typeName(const char *)
33 return "const char *";
36 const char *typeName(const std::string
&)
41 template <typename Str1T
, typename Str2T
>
42 void testEq(Str1T
&& lhs
, Str2T
&& rhs
)
44 BT_ASSERT(asConstCharPtr(lhs
) != asConstCharPtr(rhs
));
45 ok(lhs
== rhs
, "`%s` == `%s`", typeName(lhs
), typeName(rhs
));
48 template <typename Str1T
, typename Str2T
>
49 void testNe(Str1T
&& lhs
, Str2T
&& rhs
)
51 BT_ASSERT(asConstCharPtr(lhs
) != asConstCharPtr(rhs
));
52 ok(lhs
!= rhs
, "`%s` != `%s`", typeName(lhs
), typeName(rhs
));
57 const std::string foo1
= "foo", foo2
= "foo";
58 const std::string bar
= "bar";
60 /* `CStringView` vs `CStringView` */
61 testEq(bt2c::CStringView
{foo1
}, bt2c::CStringView
{foo2
});
62 testNe(bt2c::CStringView
{foo1
}, bt2c::CStringView
{bar
});
64 /* `CStringView` vs `const char *` */
65 testEq(bt2c::CStringView
{foo1
}, foo2
.c_str());
66 testNe(bt2c::CStringView
{foo1
}, bar
.c_str());
67 testEq(foo1
.c_str(), bt2c::CStringView
{foo2
});
68 testNe(foo1
.c_str(), bt2c::CStringView
{bar
});
70 /* `StringView` vs `std::string` */
71 testEq(bt2c::CStringView
{foo1
}, foo2
);
72 testNe(bt2c::CStringView
{foo1
}, bar
);
73 testEq(foo1
, bt2c::CStringView
{foo2
});
74 testNe(foo1
, bt2c::CStringView
{bar
});
79 ok(bt2c::CStringView
{"Moutarde choux"}.startsWith("Moutarde"),
80 "\"Moutarde Choux\" starts with \"Moutarde\"");
81 ok(!bt2c::CStringView
{"Moutarde choux"}.startsWith("Choux"),
82 "\"Moutarde Choux\" does not start with \"Choux\"");
83 ok(bt2c::CStringView
{"Moutarde choux"}.startsWith(""), "\"Moutarde Choux\" starts with \"\"");
84 ok(bt2c::CStringView
{"Moutarde choux"}.startsWith("Moutarde choux"),
85 "\"Moutarde Choux\" starts with \"Moutarde choux\"");
86 ok(!bt2c::CStringView
{"Moutarde"}.startsWith("Moutarde choux"),
87 "\"Moutarde\" does not start with \"Moutarde choux\"");
88 ok(bt2c::CStringView
{""}.startsWith(""), "\"\" starts with \"\"");
This page took 0.032269 seconds and 5 git commands to generate.