-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathtest-time.js
207 lines (165 loc) · 5.81 KB
/
test-time.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) 2018 Intel Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
const assert = require('assert');
const rclnodejs = require('../index.js');
const { Time, Clock, Duration } = rclnodejs;
const { ClockType } = Clock;
describe('rclnodejs Time/Clock testing', function () {
this.timeout(60 * 1000);
before(function () {
return rclnodejs.init();
});
after(function () {
rclnodejs.shutdown();
});
it('Construct time object', function () {
let time = new Time(1, 64);
assert.strictEqual(time.nanoseconds, 1000000064);
assert.strictEqual(time.clockType, ClockType.SYSTEM_TIME);
time = new Time(0, Number.MAX_SAFE_INTEGER);
assert.strictEqual(time.nanoseconds, 9007199254740991);
// The nanoseconds property will be presented in a string, if the value excesses 2^53-1
time = new Time(0, '9007199254740992');
assert.strictEqual(time.nanoseconds, '9007199254740992');
time = new Time(0, '9223372036854775807');
assert.strictEqual(time.nanoseconds, '9223372036854775807');
time = Time.fromMsg({ sec: 1, nanosec: 64 });
assert.strictEqual(time.nanoseconds, 1000000064);
assert.strictEqual(time.clockType, ClockType.ROS_TIME);
assert.throws(() => {
new Time(1, 1, 'SYSTEM_TIME');
}, TypeError);
assert.throws(() => {
new Time({ seconds: 0, nanoseconds: 0 });
}, TypeError);
assert.throws(() => {
new Time(-1, 0);
}, RangeError);
assert.throws(() => {
new Time(0, '-9007199254740992');
}, RangeError);
assert.throws(() => {
new Time(0, -1);
}, RangeError);
});
it('Construct duration object', function () {
let duration = new Duration();
assert.strictEqual(duration.nanoseconds, 0);
duration = new Duration(1, 64);
assert.strictEqual(duration.nanoseconds, 1000000064);
duration = new Duration(-1);
assert.strictEqual(duration.nanoseconds, -1000000000);
duration = new Duration(0, -1);
assert.strictEqual(duration.nanoseconds, -1);
duration = new Duration(0, Number.MAX_SAFE_INTEGER);
assert.strictEqual(duration.nanoseconds, 9007199254740991);
duration = new Duration(0, '9007199254740992');
assert.strictEqual(duration.nanoseconds, '9007199254740992');
duration = new Duration(0, '-9007199254740992');
assert.strictEqual(duration.nanoseconds, '-9007199254740992');
duration = new Duration(0, '9223372036854775807');
assert.strictEqual(duration.nanoseconds, '9223372036854775807');
});
it('Test time functions', function () {
let left = new Time(0, 1);
let right = new Time(0, 2);
assert.strictEqual(left.eq(right), false);
assert.strictEqual(left.ne(right), true);
assert.strictEqual(left.lt(right), true);
assert.strictEqual(left.lte(right), true);
assert.strictEqual(left.gt(right), false);
assert.strictEqual(left.gte(right), false);
left = new Time(0, 1, ClockType.SYSTEM_TIME);
right = new Time(0, 2, ClockType.STEADY_TIME);
assert.throws(() => {
left.eq(right);
}, TypeError);
assert.throws(() => {
left.ne(right);
}, TypeError);
assert.throws(() => {
left.lt(right);
}, TypeError);
assert.throws(() => {
left.lte(right);
}, TypeError);
assert.throws(() => {
left.gt(right);
}, TypeError);
assert.throws(() => {
left.gte(right);
}, TypeError);
let time = new Time(0, 1, ClockType.STEADY_TIME);
let duration = new Duration(0, 1);
let result = time.add(duration);
assert.ok(result instanceof Time);
assert.strictEqual(result.nanoseconds, 2);
assert.strictEqual(result.clockType, ClockType.STEADY_TIME);
result = time.sub(duration);
assert.ok(result instanceof Time);
assert.strictEqual(result.nanoseconds, 0);
assert.strictEqual(result.clockType, ClockType.STEADY_TIME);
let diff = time.sub(result);
assert.ok(diff instanceof Duration);
assert.strictEqual(diff.nanoseconds, 1);
assert.throws(() => {
time.add(result);
}, TypeError);
let nanos = time._nanoseconds;
time.secondsAndNanoseconds;
assert.strictEqual(time._nanoseconds, nanos);
});
it('Test duration functions', function () {
let left = new Duration(0, 1);
let right = new Duration(0, 2);
assert.strictEqual(left.eq(right), false);
assert.strictEqual(left.ne(right), true);
assert.strictEqual(left.gt(right), false);
assert.strictEqual(left.gte(right), false);
assert.strictEqual(left.lt(right), true);
assert.strictEqual(left.lte(right), true);
left = new Duration(0, 5e9);
right = new Duration(5, 0);
assert.ok(left.eq(right));
assert.throws(() => {
left.eq(5e9);
}, TypeError);
let time = new Time();
assert.throws(() => {
left.eq(time);
}, TypeError);
assert.throws(() => {
left.ne(time);
}, TypeError);
assert.throws(() => {
left.gt(time);
}, TypeError);
assert.throws(() => {
left.gte(time);
}, TypeError);
assert.throws(() => {
left.lt(time);
}, TypeError);
assert.throws(() => {
left.lte(time);
}, TypeError);
});
it('Conversion to Time message', function () {
let time = new Time(100, 200);
let msg = time.toMsg();
assert.strictEqual(msg.sec, 100);
assert.strictEqual(msg.nanosec, 200);
});
});