Commit 379a4b15 authored by rna's avatar rna

Add PrrtTimestamp_cmp method.

parent 660edc66
......@@ -15,7 +15,7 @@ add_subdirectory(proto)
add_subdirectory(util)
add_executable(sender sender.c)
add_executable(receiver receiver.c ../tests/common.h)
add_executable(receiver receiver.c)
target_link_libraries(sender LINK_PUBLIC PRRT UTIL ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(receiver LINK_PUBLIC PRRT UTIL ${CMAKE_THREAD_LIBS_INIT})
......@@ -21,3 +21,14 @@ struct timespec abstime_from_now(uint32_t wait_time) {
return deadline;
}
// < 0: a less than b (b is in the future)
// > 0: a greater b (b is in the past)
// == 0: a equal b
int64_t PrrtTimestamp_cmp(prrtTimestamp_t a, prrtTimestamp_t b) {
prrtTimestamp_t diff = (prrtTimestamp_t) (a - b);
if (diff < TIMESTAMP_SPACE / 2) {
return diff;
} else {
return ((int64_t)diff) - ((int64_t)(TIMESTAMP_SPACE) + 1);
}
}
\ No newline at end of file
#ifndef PRRT_TIME_H
#define PRRT_TIME_H
#include "../proto/types/packet.h"
long long timedelta(struct timespec *t1, struct timespec *t2);
struct timespec abstime_from_now(uint32_t wait_time);
int64_t PrrtTimestamp_cmp(prrtTimestamp_t a, prrtTimestamp_t b);
#endif //PRRT_TIME_H
add_subdirectory(lib/gtest-1.8.0)
include_directories(SYSTEM ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
add_executable(prrtTests common.h bitmap_tests.cpp receptionTable_tests.cpp delivered_packet_table_tests.cpp bptree_tests.cpp PrrtBlock_tests.cpp)
add_executable(prrtTests common.h util_tests.cpp bitmap_tests.cpp receptionTable_tests.cpp delivered_packet_table_tests.cpp bptree_tests.cpp PrrtBlock_tests.cpp)
target_link_libraries(prrtTests LINK_PUBLIC gtest PRRT UTIL gtest_main)
\ No newline at end of file
#include "common.h"
extern "C" {
#include "prrt/util/time.h"
}
class UtilTests : public ::testing::Test {
protected:
virtual void SetUp()
{
}
virtual void TearDown()
{
}
};
TEST_F(UtilTests, Timestamps)
{
prrtTimestamp_t a = 5;
prrtTimestamp_t b = 47;
prrtTimestamp_t c = 4294967295;
ASSERT_EQ(PrrtTimestamp_cmp(a,b), -42);
ASSERT_EQ(PrrtTimestamp_cmp(b,a), 42);
ASSERT_EQ(PrrtTimestamp_cmp(c,a), -6);
ASSERT_EQ(PrrtTimestamp_cmp(a,c), 6);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment