Revision e19ad051

View differences:

CMakeLists.txt
234 234
    if(TARGET ${PROJECT_NAME}-test-client)
235 235
    target_link_libraries(${PROJECT_NAME}-test-client ${PROJECT_NAME})
236 236
    endif()
237
    catkin_add_gtest(${PROJECT_NAME}-test-timestamp test/timestamp.cpp)
238
    if(TARGET ${PROJECT_NAME}-test-timestamp)
239
    target_link_libraries(${PROJECT_NAME}-test-timestamp ${PROJECT_NAME})
240
    endif()
237 241
ENDIF (catkin_FOUND)
238 242

  
239 243

  
src/timestamp.cpp
21 21
}
22 22

  
23 23
Timestamp Timestamp::now(void){
24
    Timestamp res;
25 24
    struct timespec tp;
26 25
    clock_gettime(CLOCK_REALTIME, &tp);
27
    res.set(tp.tv_sec, tp.tv_nsec);
28
    return res;
26
    return Timestamp(tp.tv_sec, tp.tv_nsec);
29 27
}
30 28

  
31 29
void Timestamp::set(int32_t _sec, int32_t _nsec){
test/client.cpp
1 1
// Bring in my package's API, which is what I'm testing
2
#include "client.h"
2
#include "humotion/client/client.h"
3 3
#include <gtest/gtest.h>
4 4
#include <string>
5 5
#include <cstdio>
6 6
using namespace std;
7
using namespace humotion;
8
using namespace humotion::client;
7 9

  
8 10
namespace {
9 11

  
......
11 13
class client_Test : public ::testing::Test {
12 14
protected:
13 15
    client_Test() {
14
		//set-up work for EACH test here
15
        s = new client();
16
        //set-up work for EACH test here
17
        s = new Client("test", "ROS");
18

  
19
    }
16 20

  
17
	}
18
	
19 21
    virtual ~client_Test() {
20
		//clean-up work that doesn't throw exceptions here
21
		delete(s);
22
		s = NULL;
23
	}
24
	
25
	//if the constructor and destructor are not enough for setting up
26
	//and cleaning up each test, you can define the following methods:
27
	virtual void SetUp() {
28
		//code here will be called immediately after the constructor (right before each test).
29
	}
22
        //clean-up work that doesn't throw exceptions here
23
        delete(s);
24
        s = NULL;
25
    }
30 26

  
31
	virtual void TearDown() {
32
		//code here will be called immediately after each test (right
33
		//before the destructor).
34
	}
27
    //if the constructor and destructor are not enough for setting up
28
    //and cleaning up each test, you can define the following methods:
29
    virtual void SetUp() {
30
        //code here will be called immediately after the constructor (right before each test).
31
    }
32

  
33
    virtual void TearDown() {
34
        //code here will be called immediately after each test (right
35
        //before the destructor).
36
    }
35 37

  
36
	//ojects declared here can be used by all tests in the test case for Foo
37
    client *s;
38
    //ojects declared here can be used by all tests in the test case for Foo
39
    Client *s;
38 40
};
39 41

  
40 42
// Tests that the Foo::Bar() method does Abc.
......
64 66

  
65 67
// Run all the tests that were declared with TEST()
66 68
int main(int argc, char **argv){
67
	testing::InitGoogleTest(&argc, argv);
68
	return RUN_ALL_TESTS();
69
    testing::InitGoogleTest(&argc, argv);
70
    return RUN_ALL_TESTS();
69 71
}
test/server.cpp
1 1
// Bring in my package's API, which is what I'm testing
2
#include "server.h"
2
#include "humotion/server/server.h"
3 3
#include <gtest/gtest.h>
4 4
#include <string>
5 5
#include <cstdio>
6 6
using namespace std;
7
using namespace humotion;
8
using namespace humotion::server;
7 9

  
8 10
namespace {
9 11

  
......
12 14
protected:
13 15
	server_Test() {
14 16
		//set-up work for EACH test here
15
		s = new Server();
17
        s = new Server("test", "ROS", NULL);
16 18

  
17 19
	}
18 20
	
......
50 52
	EXPECT_EQ(s->ok(), true);
51 53
	
52 54
	for(int i=0; i<100000; i++){
53
		s->tick();
55
        //s->tick();
54 56
	}
55 57
}
56 58

  
test/timestamp.cpp
1
// Bring in my package's API, which is what I'm testing
2
#include "timestamp.h"
3
#include <gtest/gtest.h>
4
#include <string>
5
#include <cstdio>
6
#include <stdio.h>
7
#include <unistd.h>
8

  
9
using namespace std;
10
using namespace humotion;
11

  
12
namespace {
13

  
14
//the fixture for testing class Foo.
15
class timestamp_Test : public ::testing::Test {
16
protected:
17
    timestamp_Test() {
18
        //set-up work for EACH test here
19
    }
20

  
21
    virtual ~timestamp_Test() {
22
        //clean-up work that doesn't throw exceptions here
23
    }
24

  
25
    //if the constructor and destructor are not enough for setting up
26
    //and cleaning up each test, you can define the following methods:
27
    virtual void SetUp() {
28
        //code here will be called immediately after the constructor (right before each test).
29
    }
30

  
31
    virtual void TearDown() {
32
        //code here will be called immediately after each test (right
33
        //before the destructor).
34
    }
35

  
36
    //ojects declared here can be used by all tests in the test case for Foo
37
};
38

  
39
// Tests that the Foo::Bar() method does Abc.
40
TEST_F(timestamp_Test, init_from_values) {
41

  
42
    //check sec/nsec initialiser
43
    Timestamp a(1.0, 0.0);
44
    ASSERT_EQ(a.to_seconds(), 1.0);
45

  
46
    Timestamp b(999, 123);
47
    ASSERT_EQ(b.to_seconds(), 999.0 + 123 / 1000000000.0);
48

  
49
    Timestamp c(666.777);
50
    ASSERT_EQ(c.to_seconds(), 666.777);
51
    ASSERT_EQ(c.sec, 666);
52
    ASSERT_EQ(c.nsec, 0.777 / 1E-9);;
53

  
54
    //test initializer with now()
55
    struct timespec tp;
56
    clock_gettime(CLOCK_REALTIME, &tp);
57

  
58
    Timestamp ts_d;
59
    sleep(1);
60
    Timestamp ts_e;
61

  
62
    //they should be at least 1 second apart:
63
    double diff = ts_e.to_seconds() - ts_d.to_seconds();
64
    ASSERT_GE(diff, 1.0);
65
    ASSERT_LE(diff, 2.0);
66

  
67
    //d should also be greater equal that timespec values:
68
    ASSERT_GE(ts_d.sec, tp.tv_sec);
69

  
70
    SUCCEED();
71
}
72

  
73
TEST_F(timestamp_Test, comparison) {
74

  
75
    //check sec/nsec initialiser
76
    Timestamp a(1234567, 89101213);
77
    Timestamp b(1234567, 89101213);
78
    ASSERT_TRUE(a == b);
79
    ASSERT_TRUE(a >= b);
80
    ASSERT_TRUE(a <= b);
81
    ASSERT_FALSE(a != b);
82
    ASSERT_FALSE(a < b);
83
    ASSERT_FALSE(a > b);
84

  
85
    //check double initializer
86
    Timestamp e(1.234567);
87
    Timestamp f(1.234568);
88
    ASSERT_FALSE(e == f);
89
    ASSERT_FALSE(e >= f);
90
    ASSERT_TRUE(e <= f);
91
    ASSERT_TRUE(e != f);
92
    ASSERT_TRUE(e < f);
93
    ASSERT_FALSE(e > f);
94

  
95
    //check now() initializer
96
    Timestamp c;
97
    sleep(1);
98
    Timestamp d;
99
    ASSERT_FALSE(c == d);
100
    ASSERT_FALSE(c >= d);
101
    ASSERT_TRUE(c <= d);
102
    ASSERT_TRUE(c != d);
103
    ASSERT_TRUE(c < d);
104
    ASSERT_FALSE(c > d);
105

  
106
    //ok now test comparisons based on sec:
107
    Timestamp g(100, 1234567);
108
    Timestamp h(101, 1234567);
109
    ASSERT_FALSE(g == h);
110
    ASSERT_FALSE(g >= h);
111
    ASSERT_TRUE(g <= h);
112
    ASSERT_TRUE(g != h);
113
    ASSERT_TRUE(g < h);
114
    ASSERT_FALSE(g > h);
115

  
116
    //ok now test comparisons based on nsec:
117
    {
118
        Timestamp i(100, 1234567);
119
        Timestamp j(100, 1234568);
120
        ASSERT_FALSE(i == j);
121
        ASSERT_FALSE(i >= j);
122
        ASSERT_TRUE(i <= j);
123
        ASSERT_TRUE(i != j);
124
        ASSERT_TRUE(i < j);
125
        ASSERT_FALSE(i > j);
126
    }
127

  
128
    //ok now test comparisons based on sec + nsec:
129
    {
130
        Timestamp i(100, 1234568);
131
        Timestamp j(101, 1234567);
132
        ASSERT_FALSE(i == j);
133
        ASSERT_FALSE(i >= j);
134
        ASSERT_TRUE(i <= j);
135
        ASSERT_TRUE(i != j);
136
        ASSERT_TRUE(i < j);
137
        ASSERT_FALSE(i > j);
138
    }
139

  
140
    SUCCEED();
141
}
142

  
143
// Tests that Foo does Xyz.
144
// TEST_F(xsc3_client_cpp_Test, DoesXyz) {
145
// 	CheckConnection(xsc3_client);
146
// }
147

  
148
}  // namespace
149

  
150
// Run all the tests that were declared with TEST()
151
int main(int argc, char **argv){
152
    testing::InitGoogleTest(&argc, argv);
153
    return RUN_ALL_TESTS();
154
}

Also available in: Unified diff