Skip to content

Commit

Permalink
Migrate parse_time to Catch2
Browse files Browse the repository at this point in the history
  • Loading branch information
mmd-osm committed Nov 25, 2023
1 parent de65a07 commit 6dca62c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 40 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ ___test_test_http_CPPFLAGS=$(AM_CPPFLAGS) $(CATCH2_CPPFLAGS)
___test_test_http_SOURCES=\
../test/test_http.cpp

___test_test_parse_time_CPPFLAGS=$(AM_CPPFLAGS) $(CATCH2_CPPFLAGS)
___test_test_parse_time_SOURCES=\
../test/test_parse_time.cpp

Expand Down
53 changes: 13 additions & 40 deletions test/test_parse_time.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,22 @@
#include "cgimap/time.hpp"

#include <ctime>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <iomanip>
#include <iostream>
#include <time.h>

#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>

void assert_eq_time(std::chrono::system_clock::time_point expected, std::string str) {
const std::chrono::system_clock::time_point tb = parse_time(str);
if (expected != tb) {
std::time_t time_ex = std::chrono::system_clock::to_time_t(expected);
std::time_t time_tb = std::chrono::system_clock::to_time_t(tb);
TEST_CASE("Parse time", "[time]")
{
struct tm dat{};
dat.tm_year = 2015 - 1900;
dat.tm_mon = 8 - 1;
dat.tm_mday = 31;
dat.tm_hour = 23;
dat.tm_min = 40;
dat.tm_sec = 10;

std::ostringstream ostr;
ostr << "Expected " << std::put_time( std::gmtime( &time_ex ), "%FT%T") << ", but got time "
<< std::put_time( std::gmtime( &time_tb ), "%FT%T") << " [from '" << str << "'] instead.";
throw std::runtime_error(ostr.str());
}
}

int main(int argc, char *argv[]) {
try {
struct tm dat{};
dat.tm_year = 2015 - 1900;
dat.tm_mon = 8 - 1;
dat.tm_mday = 31;
dat.tm_hour = 23;
dat.tm_min = 40;
dat.tm_sec = 10;

std::time_t tm_t = timegm(&dat);

assert_eq_time(std::chrono::system_clock::from_time_t(tm_t), "2015-08-31T23:40:10Z");

} catch (const std::exception &ex) {
std::cerr << "EXCEPTION: " << ex.what() << std::endl;
return 1;

} catch (...) {
std::cerr << "UNKNOWN ERROR" << std::endl;
return 1;
}
std::time_t tm_t = timegm(&dat);

return 0;
CHECK(std::chrono::system_clock::from_time_t(tm_t) == parse_time("2015-08-31T23:40:10Z"));
}

0 comments on commit 6dca62c

Please sign in to comment.