Skip to content
Snippets Groups Projects
Commit b2517279 authored by Recolic Keghart's avatar Recolic Keghart
Browse files

working on writing tests

parent f8a72d8b
No related branches found
No related tags found
No related merge requests found
{
"files.associations": {
"cxx11": "cpp",
"cxx14": "cpp",
"cxx17": "cpp",
"gcc": "cpp",
"linux": "cpp",
"win": "cpp",
"compiler_detector": "cpp",
"*.tcc": "cpp",
"string_view": "cpp",
"condition_variable": "cpp",
"thread": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"array": "cpp",
"atomic": "cpp",
"strstream": "cpp",
"bitset": "cpp",
"chrono": "cpp",
"codecvt": "cpp",
"complex": "cpp",
"cstdint": "cpp",
"exception": "cpp",
"fstream": "cpp",
"functional": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"memory": "cpp",
"mutex": "cpp",
"new": "cpp",
"numeric": "cpp",
"optional": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"utility": "cpp",
"valarray": "cpp",
"algorithm": "cpp",
"*.ipp": "cpp"
}
}
\ No newline at end of file
!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
!_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/
!_TAG_PROGRAM_NAME Exuberant Ctags //
!_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/
!_TAG_PROGRAM_VERSION 5.8 //
cc_codegen.py ../sys/cc_codegen.py 1;" kind:file line:1
genDefList ../sys/cc_codegen.py /^def genDefList(idarr):$/;" kind:function line:3
iarr ../sys/cc_codegen.py /^ iarr=i.split(' ')$/;" kind:variable line:20
osarr ../sys/cc_codegen.py /^ osarr=fd.read().split('\\n')$/;" kind:variable line:16
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
#include <sstream> #include <sstream>
namespace rlib { namespace rlib {
namespace impl { namespace impl {
// If libr.cc is built under C++17, and other header files are included in C++14 project,
// Then something wrong will happen. So DO NOT ignore the following definitions. NEVER.
//#if RLIB_CXX_STD < 2017 //#if RLIB_CXX_STD < 2017
bool enable_endl_flush = true; bool enable_endl_flush = true;
int max_predefined_log_level = (int)log_level_t::DEBUG; int max_predefined_log_level = (int)log_level_t::DEBUG;
......
...@@ -36,11 +36,11 @@ namespace rlib { ...@@ -36,11 +36,11 @@ namespace rlib {
// Allow extension. // Allow extension.
enum class log_level_t : int { FATAL = 1, ERROR, WARNING, INFO, VERBOSE, DEBUG }; enum class log_level_t : int { FATAL = 1, ERROR, WARNING, INFO, VERBOSE, DEBUG };
namespace impl { namespace impl {
//#if RLIB_CXX_STD < 2017 #if RLIB_CXX_STD < 2017
extern int max_predefined_log_level; extern int max_predefined_log_level;
//#else #else
// inline int max_predefined_log_level = (int)log_level_t::DEBUG; inline int max_predefined_log_level = (int)log_level_t::DEBUG;
//#endif #endif
} }
/* /*
How to update log_level_t: How to update log_level_t:
......
...@@ -80,11 +80,11 @@ namespace rlib { ...@@ -80,11 +80,11 @@ namespace rlib {
size_t printfln(const std::string &fmt, Args... args); size_t printfln(const std::string &fmt, Args... args);
namespace impl { namespace impl {
//#if RLIB_CXX_STD < 2017 #if RLIB_CXX_STD < 2017
extern bool enable_endl_flush; extern bool enable_endl_flush;
//#else #else
// inline bool enable_endl_flush = true; inline bool enable_endl_flush = true;
//#endif #endif
} }
inline bool sync_with_stdio(bool sync = true) noexcept { inline bool sync_with_stdio(bool sync = true) noexcept {
......
...@@ -10,13 +10,16 @@ namespace rlib { ...@@ -10,13 +10,16 @@ namespace rlib {
public: public:
int overflow(int c) { return c; } int overflow(int c) { return c; }
}; };
// stdc++ 17 removed #if RLIB_CXX_STD < 2017
extern NullStreamBuf null_streambuf; extern NullStreamBuf null_streambuf;
#else
inline NullStreamBuf null_streambuf;
#endif
} }
//#if RLIB_CXX_STD < 2017 #if RLIB_CXX_STD < 2017
extern std::ostream null_stream; extern std::ostream null_stream;
//#else #else
// inline std::ostream null_stream(&impl::null_streambuf); inline std::ostream null_stream(&impl::null_streambuf);
//#endif #endif
} }
...@@ -28,14 +28,14 @@ namespace rlib { ...@@ -28,14 +28,14 @@ namespace rlib {
// literals::_format, format_string, string::format // literals::_format, format_string, string::format
namespace impl { namespace impl {
#ifndef RLIB_MINGW_DISABLE_TLS #ifndef RLIB_MINGW_DISABLE_TLS
//#if RLIB_CXX_STD < 2017 #if RLIB_CXX_STD < 2017
// Intel C++ compiler has a pending bug for `thread_local inline` variable. // Intel C++ compiler has a pending bug for `thread_local inline` variable.
thread_local extern std::stringstream to_string_by_sstream_ss; thread_local extern std::stringstream to_string_by_sstream_ss;
thread_local extern std::stringstream _format_string_helper_ss; thread_local extern std::stringstream _format_string_helper_ss;
//#else #else
// thread_local inline std::stringstream to_string_by_sstream_ss; thread_local inline std::stringstream to_string_by_sstream_ss;
// thread_local inline std::stringstream _format_string_helper_ss; thread_local inline std::stringstream _format_string_helper_ss;
//#endif #endif
#endif #endif
template <typename VarT> template <typename VarT>
std::string to_string_by_sstream(VarT &thing) { std::string to_string_by_sstream(VarT &thing) {
...@@ -78,12 +78,24 @@ namespace rlib { ...@@ -78,12 +78,24 @@ namespace rlib {
pos += 2; pos += 2;
} }
} }
ss << cutted_tmp_str << argsArr[current_used_arg]; if(current_used_arg >= sizeof...(args)) {
// not enough arguments.
throw std::out_of_range("format_string: not enough arguments for format string `" + fmt +
"`. Expecting at least " + std::to_string(current_used_arg+1) + ", got " +
std::to_string(sizeof...(args)));
}
ss << cutted_tmp_str << argsArr.at(current_used_arg);
pos += 2; pos += 2;
prev_pos = pos; prev_pos = pos;
++current_used_arg; ++current_used_arg;
} }
} }
if(current_used_arg != sizeof...(args)) {
// too many arguments.
throw std::out_of_range("format_string: too many arguments for format string `" + fmt +
"`. Expecting " + std::to_string(current_used_arg) + ", got " +
std::to_string(sizeof...(args)));
}
ss << fmt.substr(prev_pos); ss << fmt.substr(prev_pos);
return ss.str(); return ss.str();
} }
...@@ -91,10 +103,10 @@ namespace rlib { ...@@ -91,10 +103,10 @@ namespace rlib {
inline std::string format_string(const std::string &fmt, Args... args) { inline std::string format_string(const std::string &fmt, Args... args) {
return _format_string_helper(fmt, args...); return _format_string_helper(fmt, args...);
} }
template<> //template<>
inline std::string format_string<>(const std::string &fmt) { //inline std::string format_string<>(const std::string &fmt) {
return fmt; // return fmt;
} //}
/* /*
template<class MetaFmtArr, typename... Args> template<class MetaFmtArr, typename... Args>
...@@ -159,6 +171,9 @@ namespace rlib { ...@@ -159,6 +171,9 @@ namespace rlib {
if(empty()) return T(); if(empty()) return T();
return T(*this); return T(*this);
} }
const char *as(as_helper<const char *>) const {
return this->c_str();
}
std::string as(as_helper<std::string>) const { std::string as(as_helper<std::string>) const {
return std::move(*this); return std::move(*this);
} }
...@@ -170,9 +185,10 @@ namespace rlib { ...@@ -170,9 +185,10 @@ namespace rlib {
throw std::invalid_argument("Can not convert rlib::string to char: size() > 1."); throw std::invalid_argument("Can not convert rlib::string to char: size() > 1.");
return size() == 0 ? '\0' : *cbegin(); return size() == 0 ? '\0' : *cbegin();
} }
unsigned char as(as_helper<unsigned char>) const { // unsigned-char conflicts with uint8_t. I'll regard it as uint8_t. ("8".as<unsigned char> == 8)
return static_cast<unsigned char>(as<char>()); //unsigned char as(as_helper<unsigned char>) const {
} // return static_cast<unsigned char>(as<char>());
//}
bool as(as_helper<bool>) const { bool as(as_helper<bool>) const {
if(*this == "true") { if(*this == "true") {
return true; return true;
...@@ -212,10 +228,10 @@ namespace rlib { ...@@ -212,10 +228,10 @@ namespace rlib {
RLIB_IMPL_GEN_AS_ALIAS(unsigned int, unsigned long) RLIB_IMPL_GEN_AS_ALIAS(unsigned int, unsigned long)
RLIB_IMPL_GEN_AS_ALIAS(unsigned short, unsigned long) RLIB_IMPL_GEN_AS_ALIAS(unsigned short, unsigned long)
//RLIB_IMPL_GEN_AS_ALIAS(uint8_t, unsigned long) RLIB_IMPL_GEN_AS_ALIAS(uint8_t, unsigned long)
RLIB_IMPL_GEN_AS_ALIAS(short, int) RLIB_IMPL_GEN_AS_ALIAS(short, int)
//RLIB_IMPL_GEN_AS_ALIAS(int8_t, int) RLIB_IMPL_GEN_AS_ALIAS(int8_t, int)
public: public:
template <typename T> template <typename T>
......
...@@ -18,6 +18,16 @@ ...@@ -18,6 +18,16 @@
MODULES=string meta trait stdio sio scope_guard MODULES=string meta trait stdio sio scope_guard
def: CXXFLAGS=-I.
A=rlib/libr.a
cxx14_all:
string_14:
$(CXX) $(CXXFLAGS) -std=c++14 src/string.cc $(A) -o src/string_14
cxx17_all:
This diff is collapsed.
..
\ No newline at end of file
#include <rlib/string.hpp>
#include <rlib/stdio.hpp>
using namespace rlib;
using namespace rlib::literals;
int main()
{
println("fuck {} at {} a.m."_format("hust", 8));
auto s = "shit {}/{}."_rs .format("???", 1.234);
println(s);
println("\\{} will be replaced but \\\\{} will be preserved like {}."_format("you"));
return 0;
}
#define CATCH_CONFIG_MAIN
#include "../catch.hpp"
#include <rlib/string.hpp>
#include <stdexcept>
using namespace rlib::literals;
TEST_CASE("literal _format", "[string_format]") {
REQUIRE("fuck {} at {} a.m."_format("hust", 8) == "fuck hust at 8 a.m.");
REQUIRE("test {}/{}"_rs .format("hi", 1.234) == "test hi/1.234");
REQUIRE("\\{},\\\\\\{},\\abc,{}."_format("you") == "{},\\\\{},\\abc,you.");
REQUIRE("{{}}, {, }{"_format("test") == "{test}, {, }{");
REQUIRE("\\{ \\} \\}{ \\{{ \\{} {}"_format("{}") == "\\{ \\} \\}{ \\{{ {} {}");
REQUIRE_THROWS_AS("\\n{}"_format(), std::out_of_range);
REQUIRE_THROWS_AS("\\n{}{}"_format("{}"), std::out_of_range);
}
TEST_CASE("rlib::string as", "[string_as]") {
REQUIRE("123"_rs .as<int>() == 123);
REQUIRE("8"_rs .as<uint8_t>() == 8);
REQUIRE("8"_rs .as<int8_t>() == 8);
REQUIRE("8"_rs .as<char>() == '8');
REQUIRE("8"_rs .as<unsigned char>() == 8); // not a bug.
REQUIRE("hello"_rs .as<std::string>() == "hello");
REQUIRE("hello"_rs .as<rlib::string>() == "hello");
REQUIRE(std::string("hello"_rs .as<const char *>()) == "hello");
REQUIRE("1"_rs .as<bool>() == true);
REQUIRE("false"_rs .as<bool>() == false);
}
TEST_CASE("rlib::string others", "[string_op]") {
rlib::string test_str = "|123||";
REQUIRE(test_str.split('|')[1] == "123");
REQUIRE(test_str.split('|')[3] == "");
REQUIRE(test_str.split_as<int>('|')[1] == 123);
REQUIRE(test_str.split_as<int>('|')[2] == 0);
REQUIRE("\r fuck you \t \n"_rs .strip() == "fuck you");
REQUIRE("hello world"_rs .strip("ld") == "hello wor");
REQUIRE(""_rs .strip("") == "");
}
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment