32
32
33
33
#import < XCTest/XCTest.h>
34
34
35
+ #import " GPBTestUtilities.h"
35
36
#import " google/protobuf/AnyTest.pbobjc.h"
36
37
37
- // A basically random interval into the future for testing with.
38
- static const NSTimeInterval kFutureOffsetInterval = 15000 ;
39
-
40
38
// Nanosecond time accuracy
41
39
static const NSTimeInterval kTimeAccuracy = 1e-9 ;
42
40
@@ -46,59 +44,117 @@ @interface WellKnownTypesTest : XCTestCase
46
44
@implementation WellKnownTypesTest
47
45
48
46
- (void )testTimeStamp {
49
- // Test Creation.
50
- NSDate *date = [NSDate date ];
51
- GPBTimestamp *timeStamp = [[GPBTimestamp alloc ] initWithDate: date];
52
- NSDate *timeStampDate = timeStamp.date ;
53
-
54
- // Comparing timeIntervals instead of directly comparing dates because date
55
- // equality requires the time intervals to be exactly the same, and the
56
- // timeintervals go through a bit of floating point error as they are
57
- // converted back and forth from the internal representation.
58
- XCTAssertEqualWithAccuracy (date.timeIntervalSince1970 ,
59
- timeStampDate.timeIntervalSince1970 ,
60
- kTimeAccuracy );
61
-
62
- NSTimeInterval time = [date timeIntervalSince1970 ];
63
- GPBTimestamp *timeStamp2 =
64
- [[GPBTimestamp alloc ] initWithTimeIntervalSince1970: time ];
65
- NSTimeInterval durationTime = timeStamp2.timeIntervalSince1970 ;
66
- XCTAssertEqualWithAccuracy (time , durationTime, kTimeAccuracy );
67
- [timeStamp release ];
68
-
69
- // Test Mutation.
70
- date = [NSDate dateWithTimeIntervalSinceNow: kFutureOffsetInterval ];
71
- timeStamp2.date = date;
72
- timeStampDate = timeStamp2.date ;
73
- XCTAssertEqualWithAccuracy (date.timeIntervalSince1970 ,
74
- timeStampDate.timeIntervalSince1970 ,
75
- kTimeAccuracy );
76
-
77
- time = date.timeIntervalSince1970 ;
78
- timeStamp2.timeIntervalSince1970 = time ;
79
- durationTime = timeStamp2.timeIntervalSince1970 ;
80
- XCTAssertEqualWithAccuracy (time , durationTime, kTimeAccuracy );
81
- [timeStamp2 release ];
47
+ // Test negative and positive values.
48
+ NSTimeInterval values[] = {
49
+ -428027599.483999967 , -1234567.0 , -0.5 , 0 , 0.75 , 54321.0 , 2468086 ,483999967
50
+ };
51
+ for (size_t i = 0 ; i < GPBARRAYSIZE (values); ++i) {
52
+ NSTimeInterval value = values[i];
53
+
54
+ // Test Creation - date.
55
+ NSDate *date = [NSDate dateWithTimeIntervalSince1970: value];
56
+ GPBTimestamp *timeStamp = [[GPBTimestamp alloc ] initWithDate: date];
57
+
58
+ XCTAssertGreaterThanOrEqual (timeStamp.nanos , 0 ,
59
+ @" Offset %f - Date: %@ " , (double )value, date);
60
+ XCTAssertLessThan (timeStamp.nanos , 1e9 ,
61
+ @" Offset %f - Date: %@ " , (double )value, date);
62
+
63
+ // Comparing timeIntervals instead of directly comparing dates because date
64
+ // equality requires the time intervals to be exactly the same, and the
65
+ // timeintervals go through a bit of floating point error as they are
66
+ // converted back and forth from the internal representation.
67
+ XCTAssertEqualWithAccuracy (value, timeStamp.date .timeIntervalSince1970 ,
68
+ kTimeAccuracy ,
69
+ @" Offset %f - Date: %@ " , (double )value, date);
70
+ [timeStamp release ];
71
+
72
+ // Test Creation - timeIntervalSince1970.
73
+ timeStamp = [[GPBTimestamp alloc ] initWithTimeIntervalSince1970: value];
74
+
75
+ XCTAssertGreaterThanOrEqual (timeStamp.nanos , 0 ,
76
+ @" Offset %f - Date: %@ " , (double )value, date);
77
+ XCTAssertLessThan (timeStamp.nanos , 1e9 ,
78
+ @" Offset %f - Date: %@ " , (double )value, date);
79
+
80
+ XCTAssertEqualWithAccuracy (value, timeStamp.timeIntervalSince1970 ,
81
+ kTimeAccuracy ,
82
+ @" Offset %f - Date: %@ " , (double )value, date);
83
+ [timeStamp release ];
84
+
85
+ // Test Mutation - date.
86
+ timeStamp = [[GPBTimestamp alloc ] init ];
87
+ timeStamp.date = date;
88
+
89
+ XCTAssertGreaterThanOrEqual (timeStamp.nanos , 0 ,
90
+ @" Offset %f - Date: %@ " , (double )value, date);
91
+ XCTAssertLessThan (timeStamp.nanos , 1e9 ,
92
+ @" Offset %f - Date: %@ " , (double )value, date);
93
+
94
+ XCTAssertEqualWithAccuracy (value, timeStamp.date .timeIntervalSince1970 ,
95
+ kTimeAccuracy ,
96
+ @" Offset %f - Date: %@ " , (double )value, date);
97
+ [timeStamp release ];
98
+
99
+ // Test Mutation - timeIntervalSince1970.
100
+ timeStamp = [[GPBTimestamp alloc ] init ];
101
+ timeStamp.timeIntervalSince1970 = value;
102
+
103
+ XCTAssertGreaterThanOrEqual (timeStamp.nanos , 0 ,
104
+ @" Offset %f - Date: %@ " , (double )value, date);
105
+ XCTAssertLessThan (timeStamp.nanos , 1e9 ,
106
+ @" Offset %f - Date: %@ " , (double )value, date);
107
+
108
+ XCTAssertEqualWithAccuracy (value, timeStamp.date .timeIntervalSince1970 ,
109
+ kTimeAccuracy ,
110
+ @" Offset %f - Date: %@ " , (double )value, date);
111
+
112
+ [timeStamp release ];
113
+ }
82
114
}
83
115
84
116
- (void )testDuration {
85
- // Test Creation.
86
- NSTimeInterval time = [[NSDate date ] timeIntervalSince1970 ];
87
- GPBDuration *duration =
88
- [[GPBDuration alloc ] initWithTimeIntervalSince1970: time ];
89
- NSTimeInterval durationTime = duration.timeIntervalSince1970 ;
90
- XCTAssertEqualWithAccuracy (time , durationTime, kTimeAccuracy );
91
- [duration release ];
92
-
93
- // Test Mutation.
94
- GPBDuration *duration2 =
95
- [[GPBDuration alloc ] initWithTimeIntervalSince1970: time ];
96
- NSDate *date = [NSDate dateWithTimeIntervalSinceNow: kFutureOffsetInterval ];
97
- time = date.timeIntervalSince1970 ;
98
- duration2.timeIntervalSince1970 = time ;
99
- durationTime = duration2.timeIntervalSince1970 ;
100
- XCTAssertEqualWithAccuracy (time , durationTime, kTimeAccuracy );
101
- [duration2 release ];
117
+ // Test negative and positive values.
118
+ NSTimeInterval values[] = { -1000.0001 , -500.0 , -0.5 , 0 , 0.75 , 1000.0 , 2000.0002 };
119
+ for (size_t i = 0 ; i < GPBARRAYSIZE (values); ++i) {
120
+ NSTimeInterval value = values[i];
121
+
122
+ // Test Creation.
123
+ GPBDuration *duration =
124
+ [[GPBDuration alloc ] initWithTimeInterval: value];
125
+ XCTAssertEqualWithAccuracy (value, duration.timeInterval , kTimeAccuracy ,
126
+ @" For interval %f " , (double )value);
127
+ if (value > 0 ) {
128
+ XCTAssertGreaterThanOrEqual (duration.seconds , 0 ,
129
+ @" For interval %f " , (double )value);
130
+ XCTAssertGreaterThanOrEqual (duration.nanos , 0 ,
131
+ @" For interval %f " , (double )value);
132
+ } else {
133
+ XCTAssertLessThanOrEqual (duration.seconds , 0 ,
134
+ @" For interval %f " , (double )value);
135
+ XCTAssertLessThanOrEqual (duration.nanos , 0 ,
136
+ @" For interval %f " , (double )value);
137
+ }
138
+ [duration release ];
139
+
140
+ // Test Mutation.
141
+ duration = [[GPBDuration alloc ] init ];
142
+ duration.timeInterval = value;
143
+ XCTAssertEqualWithAccuracy (value, duration.timeInterval , kTimeAccuracy ,
144
+ @" For interval %f " , (double )value);
145
+ if (value > 0 ) {
146
+ XCTAssertGreaterThanOrEqual (duration.seconds , 0 ,
147
+ @" For interval %f " , (double )value);
148
+ XCTAssertGreaterThanOrEqual (duration.nanos , 0 ,
149
+ @" For interval %f " , (double )value);
150
+ } else {
151
+ XCTAssertLessThanOrEqual (duration.seconds , 0 ,
152
+ @" For interval %f " , (double )value);
153
+ XCTAssertLessThanOrEqual (duration.nanos , 0 ,
154
+ @" For interval %f " , (double )value);
155
+ }
156
+ [duration release ];
157
+ }
102
158
}
103
159
104
160
- (void )testAnyHelpers {
0 commit comments