robot_time/test/walltime_standalone_test.cpp

281 lines
7.4 KiB
C++

/*********************************************************************
* Standalone WallTime Library Test
*
* Test file for the header-only WallTime library
*********************************************************************/
#include <robot/walltime.h>
#include <iostream>
#include <cassert>
#include <cmath>
void test_walltime_basic()
{
std::cout << "Testing basic WallTime operations..." << std::endl;
// Test default constructor
robot::WallTime t1;
assert(t1.sec == 0);
assert(t1.nsec == 0);
assert(t1.isZero());
// Test constructor with values
robot::WallTime t2(1234567890, 123456789);
assert(t2.sec == 1234567890);
assert(t2.nsec == 123456789);
// Test constructor from double
robot::WallTime t3(1234567890.123456789);
assert(std::abs(t3.toSec() - 1234567890.123456789) < 1e-6);
std::cout << " ✓ Basic operations passed" << std::endl;
}
void test_walltime_now()
{
std::cout << "Testing WallTime::now()..." << std::endl;
robot::WallTime t1 = robot::WallTime::now();
robot::WallTime t2 = robot::WallTime::now();
// t2 should be >= t1
assert(t2 >= t1);
// Should be system time
assert(robot::WallTime::isSystemTime());
std::cout << " ✓ WallTime::now() passed" << std::endl;
}
void test_wallduration_basic()
{
std::cout << "Testing basic WallDuration operations..." << std::endl;
// Test default constructor
robot::WallDuration d1;
assert(d1.sec == 0);
assert(d1.nsec == 0);
assert(d1.isZero());
// Test constructor with values
robot::WallDuration d2(5, 123456789);
assert(d2.sec == 5);
assert(d2.nsec == 123456789);
// Test constructor from double
robot::WallDuration d3(5.123456789);
assert(std::abs(d3.toSec() - 5.123456789) < 1e-6);
std::cout << " ✓ Basic operations passed" << std::endl;
}
void test_wallduration_normalize()
{
std::cout << "Testing WallDuration normalization..." << std::endl;
// Test overflow nanoseconds
robot::WallDuration d1(0, 2000000000);
assert(d1.sec == 2);
assert(d1.nsec == 0);
// Test negative nanoseconds
robot::WallDuration d2(5, -500000000);
assert(d2.sec == 4);
assert(d2.nsec == 500000000);
std::cout << " ✓ Normalization passed" << std::endl;
}
void test_arithmetic()
{
std::cout << "Testing arithmetic operations..." << std::endl;
robot::WallTime t1(1000, 0);
robot::WallDuration d1(5, 0);
// Test addition
robot::WallTime t2 = t1 + d1;
assert(t2.sec == 1005);
assert(t2.nsec == 0);
// Test subtraction
robot::WallTime t3 = t2 - d1;
assert(t3.sec == 1000);
assert(t3.nsec == 0);
// Test duration subtraction
robot::WallDuration d2 = t2 - t1;
assert(d2.sec == 5);
assert(d2.nsec == 0);
// Test compound assignment
robot::WallTime t4 = t1;
t4 += d1;
assert(t4.sec == 1005);
t4 -= d1;
assert(t4.sec == 1000);
std::cout << " ✓ Arithmetic operations passed" << std::endl;
}
void test_comparison()
{
std::cout << "Testing comparison operations..." << std::endl;
robot::WallTime t1(1000, 0);
robot::WallTime t2(1000, 500000000);
robot::WallTime t3(2000, 0);
// Test equality
assert(t1 == t1);
assert(t1 != t2);
// Test less than
assert(t1 < t2);
assert(t1 < t3);
// Test greater than
assert(t2 > t1);
assert(t3 > t1);
// Test less than or equal
assert(t1 <= t1);
assert(t1 <= t2);
// Test greater than or equal
assert(t2 >= t1);
assert(t2 >= t2);
std::cout << " ✓ Comparison operations passed" << std::endl;
}
void test_conversion()
{
std::cout << "Testing conversion operations..." << std::endl;
robot::WallTime t(1234567890, 123456789);
// Test toSec
double seconds = t.toSec();
assert(std::abs(seconds - 1234567890.123456789) < 1e-6);
// Test toNSec
uint64_t nanoseconds = t.toNSec();
assert(nanoseconds == 1234567890123456789ULL);
// Test fromSec
robot::WallTime t2;
t2.fromSec(1234567890.123456789);
assert(std::abs(t2.toSec() - 1234567890.123456789) < 1e-6);
// Test fromNSec
robot::WallTime t3;
t3.fromNSec(1234567890123456789ULL);
assert(t3.sec == 1234567890);
assert(t3.nsec == 123456789);
std::cout << " ✓ Conversion operations passed" << std::endl;
}
void test_sleep()
{
std::cout << "Testing sleep operations..." << std::endl;
robot::WallTime start = robot::WallTime::now();
// Test WallDuration::sleep()
robot::WallDuration sleep_duration(0.1); // 100ms
bool sleep_result = sleep_duration.sleep();
assert(sleep_result);
robot::WallTime end = robot::WallTime::now();
robot::WallDuration elapsed = end - start;
// Should have slept at least 100ms
assert(elapsed.toSec() >= 0.09); // Allow some tolerance
// Test WallTime::sleepUntil()
robot::WallTime target = robot::WallTime::now() + robot::WallDuration(0.1);
bool sleep_until_result = robot::WallTime::sleepUntil(target);
assert(sleep_until_result);
robot::WallTime after = robot::WallTime::now();
assert(after >= target);
std::cout << " ✓ Sleep operations passed" << std::endl;
}
void test_measurement()
{
std::cout << "Testing time measurement..." << std::endl;
robot::WallTime start = robot::WallTime::now();
// Simulate some work
double sum = 0.0;
for (int i = 0; i < 100000; ++i)
{
sum += i;
}
robot::WallTime end = robot::WallTime::now();
robot::WallDuration elapsed = end - start;
// Should have taken some time
assert(elapsed.toSec() >= 0.0);
assert(elapsed.toSec() < 1.0); // Should be fast
std::cout << " ✓ Time measurement passed (elapsed: " << elapsed.toSec() << "s)" << std::endl;
}
void test_constants()
{
std::cout << "Testing constants..." << std::endl;
// Test WallTime constants
assert(robot::WallTime::ZERO.isZero());
assert(robot::WallTime::MAX > robot::WallTime::ZERO);
assert(robot::WallTime::MIN < robot::WallTime::MAX);
// Test WallDuration constants
assert(robot::WallDuration::ZERO.isZero());
assert(robot::WallDuration::MAX > robot::WallDuration::ZERO);
assert(robot::WallDuration::MIN < robot::WallDuration::ZERO);
std::cout << " ✓ Constants passed" << std::endl;
}
int main()
{
std::cout << "========================================" << std::endl;
std::cout << "WallTime Standalone Library Tests" << std::endl;
std::cout << "========================================" << std::endl;
try
{
test_walltime_basic();
test_walltime_now();
test_wallduration_basic();
test_wallduration_normalize();
test_arithmetic();
test_comparison();
test_conversion();
test_sleep();
test_measurement();
test_constants();
std::cout << "\n========================================" << std::endl;
std::cout << "All tests passed!" << std::endl;
std::cout << "========================================" << std::endl;
return 0;
}
catch (const std::exception& e)
{
std::cerr << "Test failed with exception: " << e.what() << std::endl;
return 1;
}
}