summaryrefslogtreecommitdiff
path: root/platform/ios/test/MGLNSDataAdditionsTests.m
blob: 8d145be4a0c3267d8682f17f1eb000f45b071645 (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
#import <XCTest/XCTest.h>

#import "../../darwin/src/NSData+MGLAdditions.h"

@interface MGLNSDataAdditionsTests : XCTestCase
@end

@implementation MGLNSDataAdditionsTests

- (void)testCompressDecompress
{
    NSArray *originalArray = [self mockDataWithCount:180];

    NSData *originalData = [NSJSONSerialization dataWithJSONObject:originalArray options:0 error:nil];

    NSData *compressedData = [originalData mgl_compressedData];
    NSData *decompressedData = [compressedData mgl_decompressedData];

    NSArray *decompressedArray = [NSJSONSerialization JSONObjectWithData:decompressedData options:0 error:nil];

    XCTAssertTrue([originalArray isEqualToArray:decompressedArray], @"originalArray and decompressedArray should be equal");
}

- (NSArray *)mockDataWithCount:(NSUInteger)count
{
    NSMutableArray *array = [NSMutableArray array];

    for (NSUInteger i=0;i<count;i++)
    {
        [array addObject:@{@"lat": @([self safeValueBetween:-90 and:90]),
                           @"lng": @([self safeValueBetween:-180 and:180]),
                           @"timestamp": @((floor([NSDate date].timeIntervalSince1970) * 100) / 100)}];
    }

    return array;
}

- (double)safeValueBetween:(double)lowerBound and:(double)upperBound
{
    return floor([self randomBetween:lowerBound and:upperBound] * 100 ) / 100;
}

- (double)randomBetween:(double)lowerBound and:(double)upperBound
{
    return lowerBound * drand48() + upperBound * drand48();
}

@end