summaryrefslogtreecommitdiff
path: root/platform/darwin/test/MGLExpressionTests.mm
blob: 47315b97d6f101e2808991c0d89f9dc02223031d (plain)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#import <XCTest/XCTest.h>

#import <string>

#import "NSExpression+MGLAdditions.h"

@interface MGLExpressionTests : XCTestCase

@end

@implementation MGLExpressionTests

#pragma mark - Utility

- (NSComparisonPredicate *)equalityComparisonPredicateWithRightConstantValue:(id)rightConstantValue
{
    NSComparisonPredicate *predicate = [NSComparisonPredicate
        predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"foo"]
                    rightExpression:[NSExpression expressionForConstantValue:rightConstantValue]
                           modifier:NSDirectPredicateModifier
                               type:NSEqualToPredicateOperatorType
                            options:0];
    return predicate;
}

#pragma mark - String Tests

- (void)testExpressionConversionString
{
    NSComparisonPredicate *predicate = [self equalityComparisonPredicateWithRightConstantValue:@"bar"];
    mbgl::Value convertedValue = predicate.rightExpression.mgl_constantMBGLValue;
    XCTAssertTrue(convertedValue.is<std::string>());
    XCTAssertEqualObjects(@(convertedValue.get<std::string>().c_str()), @"bar");
}

- (void)testExpressionConversionStringWithUnicode
{
    NSComparisonPredicate *predicate = [self equalityComparisonPredicateWithRightConstantValue:@"🆔🆗🇦🇶"];
    mbgl::Value convertedValue = predicate.rightExpression.mgl_constantMBGLValue;
    XCTAssertTrue(convertedValue.is<std::string>());
    XCTAssertEqual(convertedValue.get<std::string>(), "🆔🆗🇦🇶");
}

#pragma mark - Boolean Tests

- (void)testExpressionConversionBooleanTrue
{
    NSComparisonPredicate *predicate = [self equalityComparisonPredicateWithRightConstantValue:@YES];
    mbgl::Value convertedValue = predicate.rightExpression.mgl_constantMBGLValue;
    XCTAssertTrue(convertedValue.is<bool>());
    XCTAssertEqual(convertedValue.get<bool>(), true);
}

- (void)testExpressionConversionBooleanFalse
{
    NSComparisonPredicate *predicate = [self equalityComparisonPredicateWithRightConstantValue:@NO];
    mbgl::Value convertedValue = predicate.rightExpression.mgl_constantMBGLValue;
    XCTAssertTrue(convertedValue.is<bool>());
    XCTAssertEqual(convertedValue.get<bool>(), false);
}

#pragma mark - Floating Point Tests

- (void)testExpressionConversionDouble
{
    NSComparisonPredicate *predicate;
    mbgl::Value convertedValue;

    predicate = [self equalityComparisonPredicateWithRightConstantValue:[NSNumber numberWithDouble:DBL_MIN]];
    convertedValue = predicate.rightExpression.mgl_constantMBGLValue;
    XCTAssertTrue(convertedValue.is<double>());
    XCTAssertEqual(convertedValue.get<double>(), DBL_MIN);

    predicate = [self equalityComparisonPredicateWithRightConstantValue:[NSNumber numberWithDouble:DBL_MAX]];
    convertedValue = predicate.rightExpression.mgl_constantMBGLValue;
    XCTAssertTrue(convertedValue.is<double>());
    XCTAssertEqual(convertedValue.get<double>(), DBL_MAX);
}

- (void)testExpressionConversionFloat
{
    // Because we can't guarantee precision when using float, and because
    // we warn the user to this effect in -[NSExpression mgl_constantMBGLValue],
    // we just check that things are in the ballpark here with integer values
    // and some lower-precision checks.

    NSComparisonPredicate *predicate;
    mbgl::Value convertedValue;

    predicate = [self equalityComparisonPredicateWithRightConstantValue:[NSNumber numberWithFloat:-1]];
    convertedValue = predicate.rightExpression.mgl_constantMBGLValue;
    XCTAssertTrue(convertedValue.is<double>());
    XCTAssertEqual(convertedValue.get<double>(), -1);

    predicate = [self equalityComparisonPredicateWithRightConstantValue:[NSNumber numberWithFloat:1]];
    convertedValue = predicate.rightExpression.mgl_constantMBGLValue;
    XCTAssertTrue(convertedValue.is<double>());
    XCTAssertEqual(convertedValue.get<double>(), 1);

    predicate = [self equalityComparisonPredicateWithRightConstantValue:[NSNumber numberWithFloat:-23.232342]];
    convertedValue = predicate.rightExpression.mgl_constantMBGLValue;
    XCTAssertTrue(convertedValue.is<double>());
    XCTAssertEqualWithAccuracy(convertedValue.get<double>(), -23.232342, 0.000001);

    predicate = [self equalityComparisonPredicateWithRightConstantValue:[NSNumber numberWithFloat:23.232342]];
    convertedValue = predicate.rightExpression.mgl_constantMBGLValue;
    XCTAssertTrue(convertedValue.is<double>());
    XCTAssertEqualWithAccuracy(convertedValue.get<double>(), 23.232342, 0.000001);
}

#pragma mark - Integer Tests

- (void)testExpressionNegativeIntegers
{
    NSComparisonPredicate *predicate;
    mbgl::Value convertedValue;

    NSArray<NSNumber *> *minValues = @[
        [NSNumber numberWithShort:    SHRT_MIN],
        [NSNumber numberWithInt:      INT_MIN],
        [NSNumber numberWithLong:     LONG_MIN],
        [NSNumber numberWithLongLong: LLONG_MIN],
        [NSNumber numberWithInteger:  NSIntegerMin]
    ];

    NSArray<NSNumber *> *maxValues = @[
        [NSNumber numberWithShort:    SHRT_MAX],
        [NSNumber numberWithInt:      INT_MAX],
        [NSNumber numberWithLong:     LONG_MAX],
        [NSNumber numberWithLongLong: LLONG_MAX],
        [NSNumber numberWithInteger:  NSIntegerMax]
    ];

    // Negative integers should always come back as int64_t per mbgl::Value definition.
    // We use the long long value because it can store the highest number on both 32-
    // and 64-bit and won't overflow.

    for (NSNumber *min in minValues)
    {
        predicate = [self equalityComparisonPredicateWithRightConstantValue:min];
        convertedValue = predicate.rightExpression.mgl_constantMBGLValue;
        XCTAssertTrue(convertedValue.is<int64_t>());
        XCTAssertEqual(convertedValue.get<int64_t>(), min.longLongValue);
    }

    // Positive integers should always come back as uint64_t per mbgl::Value definition.
    // We use the unsigned long long value because it can store the highest number on
    // both 32- and 64-bit and won't overflow.

    for (NSNumber *max in maxValues)
    {
        predicate = [self equalityComparisonPredicateWithRightConstantValue:max];
        convertedValue = predicate.rightExpression.mgl_constantMBGLValue;
        XCTAssertTrue(convertedValue.is<uint64_t>());
        XCTAssertEqual(convertedValue.get<uint64_t>(), max.unsignedLongLongValue);
    }

}

- (void)testExpressionPositiveAndZeroIntegers
{
    NSComparisonPredicate *predicate;
    mbgl::Value convertedValue;

    NSArray<NSNumber *> *minValues = @[
        [NSNumber numberWithUnsignedShort:    0],
        [NSNumber numberWithUnsignedInt:      0],
        [NSNumber numberWithUnsignedLong:     0],
        [NSNumber numberWithUnsignedLongLong: 0],
        [NSNumber numberWithUnsignedInteger:  0]
    ];

    NSArray<NSNumber *> *maxValues = @[
        [NSNumber numberWithUnsignedShort:    USHRT_MAX],
        [NSNumber numberWithUnsignedInt:      UINT_MAX],
        [NSNumber numberWithUnsignedLong:     ULONG_MAX],
        [NSNumber numberWithUnsignedLongLong: ULLONG_MAX],
        [NSNumber numberWithUnsignedInteger:  NSUIntegerMax]
    ];

    // Zero-value integers should always come back as uint64_t per mbgl::Value definition
    // (using the interpretation that zero is not negative). We use the unsigned long long
    // value just for parity with the positive integer test.

    for (NSNumber *min in minValues)
    {
        predicate = [self equalityComparisonPredicateWithRightConstantValue:min];
        convertedValue = predicate.rightExpression.mgl_constantMBGLValue;
        XCTAssertTrue(convertedValue.is<uint64_t>());
        XCTAssertEqual(convertedValue.get<uint64_t>(), min.unsignedLongLongValue);
    }

    // Positive integers should always come back as uint64_t per mbgl::Value definition.
    // We use the unsigned long long value because it can store the highest number on
    // both 32- and 64-bit and won't overflow.

    for (NSNumber *max in maxValues)
    {
        predicate = [self equalityComparisonPredicateWithRightConstantValue:max];
        convertedValue = predicate.rightExpression.mgl_constantMBGLValue;
        XCTAssertTrue(convertedValue.is<uint64_t>());
        XCTAssertEqual(convertedValue.get<uint64_t>(), max.unsignedLongLongValue);
    }
}

#pragma mark - Null Tests

- (void)testExpressionConversionNull
{
    NSComparisonPredicate *predicate = [self equalityComparisonPredicateWithRightConstantValue:[NSNull null]];
    mbgl::Value convertedValue = predicate.rightExpression.mgl_constantMBGLValue;
    XCTAssertTrue(convertedValue.is<mbgl::NullValue>());
}

#pragma mark - Feature type tests

- (void)testFeatureType {
    XCTAssertEqual([NSExpression expressionWithFormat:@"'Point'"].mgl_featureType, mbgl::FeatureType::Point);
    XCTAssertEqual([NSExpression expressionWithFormat:@"'LineString'"].mgl_featureType, mbgl::FeatureType::LineString);
    XCTAssertEqual([NSExpression expressionWithFormat:@"'Polygon'"].mgl_featureType, mbgl::FeatureType::Polygon);
    XCTAssertEqual([NSExpression expressionWithFormat:@"'Unknown'"].mgl_featureType, mbgl::FeatureType::Unknown);
    XCTAssertEqual([NSExpression expressionWithFormat:@"''"].mgl_featureType, mbgl::FeatureType::Unknown);
    
    XCTAssertEqual([NSExpression expressionWithFormat:@"1"].mgl_featureType, mbgl::FeatureType::Point);
    XCTAssertEqual([NSExpression expressionWithFormat:@"2"].mgl_featureType, mbgl::FeatureType::LineString);
    XCTAssertEqual([NSExpression expressionWithFormat:@"3"].mgl_featureType, mbgl::FeatureType::Polygon);
    XCTAssertEqual([NSExpression expressionWithFormat:@"0"].mgl_featureType, mbgl::FeatureType::Unknown);
    XCTAssertEqual([NSExpression expressionWithFormat:@"-1"].mgl_featureType, mbgl::FeatureType::Unknown);
    XCTAssertEqual([NSExpression expressionWithFormat:@"4"].mgl_featureType, mbgl::FeatureType::Unknown);
    
    XCTAssertEqual([NSExpression expressionWithFormat:@"nil"].mgl_featureType, mbgl::FeatureType::Unknown);
}

@end