mirror of https://github.com/oxen-io/session-ios
				
				
				
			
			You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
	
	
		
			135 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			Matlab
		
	
		
		
			
		
	
	
			135 lines
		
	
	
		
			4.4 KiB
		
	
	
	
		
			Matlab
		
	
| 
								 
											11 years ago
										 
									 | 
							
								#import <XCTest/XCTest.h>
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								#import "ObservableValue.h"
							 | 
						||
| 
								 | 
							
								#import "TestUtil.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
								@interface ObservableTest : XCTestCase
							 | 
						||
| 
								 | 
							
								@end
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								@implementation ObservableTest
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								-(void) testObservableAddRemove {
							 | 
						||
| 
								 | 
							
								    ObservableValueController* s = [ObservableValueController observableValueControllerWithInitialValue:@""];
							 | 
						||
| 
								 | 
							
								    ObservableValue* t = s;
							 | 
						||
| 
								 | 
							
								    NSMutableArray* a = [NSMutableArray array];
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
								    TOCCancelTokenSource* c = [TOCCancelTokenSource new];
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								    
							 | 
						||
| 
								 | 
							
								    [t watchLatestValueOnArbitraryThread:^(id value) {[a addObject:value];}
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
								                          untilCancelled:c.token];
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								    
							 | 
						||
| 
								 | 
							
								    test([a isEqualToArray:@[@""]]);
							 | 
						||
| 
								 | 
							
								    [s updateValue:@5];
							 | 
						||
| 
								 | 
							
								    test([a isEqualToArray:(@[@"", @5])]);
							 | 
						||
| 
								 | 
							
								    [s updateValue:@7];
							 | 
						||
| 
								 | 
							
								    test([a isEqualToArray:(@[@"", @5, @7])]);
							 | 
						||
| 
								 | 
							
								    [c cancel];
							 | 
						||
| 
								 | 
							
								    [s updateValue:@11];
							 | 
						||
| 
								 | 
							
								    test([a isEqualToArray:(@[@"", @5, @7])]);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								-(void) testObservableAddAdd {
							 | 
						||
| 
								 | 
							
								    ObservableValueController* s = [ObservableValueController observableValueControllerWithInitialValue:@""];
							 | 
						||
| 
								 | 
							
								    ObservableValue* t = s;
							 | 
						||
| 
								 | 
							
								    NSMutableArray* a = [NSMutableArray array];
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
								    TOCCancelTokenSource* c = [TOCCancelTokenSource new];
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								    
							 | 
						||
| 
								 | 
							
								    [t watchLatestValueOnArbitraryThread:^(id value) {[a addObject:value];}
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
								                          untilCancelled:c.token];
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								    [t watchLatestValueOnArbitraryThread:^(id value) {[a addObject:value];}
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
								                          untilCancelled:c.token];
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								    [t watchLatestValueOnArbitraryThread:^(id value) {[a addObject:value];}
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
								                          untilCancelled:c.token];
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								    
							 | 
						||
| 
								 | 
							
								    test([a isEqualToArray:(@[@"", @"", @""])]);
							 | 
						||
| 
								 | 
							
								    [s updateValue:@5];
							 | 
						||
| 
								 | 
							
								    test([a isEqualToArray:(@[@"", @"", @"", @5, @5, @5])]);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								-(void) testObservableRedundantSetIgnored {
							 | 
						||
| 
								 | 
							
								    id v1 = @"";
							 | 
						||
| 
								 | 
							
								    id v2 = nil;
							 | 
						||
| 
								 | 
							
								    id v3 = @1;
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    ObservableValueController* s = [ObservableValueController observableValueControllerWithInitialValue:v1];
							 | 
						||
| 
								 | 
							
								    ObservableValue* t = s;
							 | 
						||
| 
								 | 
							
								    __block id latest = nil;
							 | 
						||
| 
								 | 
							
								    __block int count = 0;
							 | 
						||
| 
								 | 
							
								    [t watchLatestValueOnArbitraryThread:^(id value) {latest = value;count++;}
							 | 
						||
| 
								 | 
							
								                          untilCancelled:nil];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    test(latest == v1);
							 | 
						||
| 
								 | 
							
								    test(count == 1);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    [s updateValue:v1];
							 | 
						||
| 
								 | 
							
								    test(latest == v1);
							 | 
						||
| 
								 | 
							
								    test(count == 1);
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    [s updateValue:v2];
							 | 
						||
| 
								 | 
							
								    test(latest == v2);
							 | 
						||
| 
								 | 
							
								    test(count == 2);
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    [s updateValue:v2];
							 | 
						||
| 
								 | 
							
								    test(latest == v2);
							 | 
						||
| 
								 | 
							
								    test(count == 2);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    [s updateValue:v1];
							 | 
						||
| 
								 | 
							
								    test(latest == v1);
							 | 
						||
| 
								 | 
							
								    test(count == 3);
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    [s updateValue:v3];
							 | 
						||
| 
								 | 
							
								    test(latest == v3);
							 | 
						||
| 
								 | 
							
								    test(count == 4);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								-(void) testObservableReentrantAdd {
							 | 
						||
| 
								 | 
							
								    ObservableValueController* s = [ObservableValueController observableValueControllerWithInitialValue:@""];
							 | 
						||
| 
								 | 
							
								    ObservableValue* t = s;
							 | 
						||
| 
								 | 
							
								    NSMutableArray* a = [NSMutableArray array];
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
								    TOCCancelTokenSource* c = [TOCCancelTokenSource new];
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								    
							 | 
						||
| 
								 | 
							
								    __block void(^registerSelf)() = nil;
							 | 
						||
| 
								 | 
							
								    void(^registerSelf_builder)() = ^{
							 | 
						||
| 
								 | 
							
								        __block bool first = true;
							 | 
						||
| 
								 | 
							
								        [t watchLatestValueOnArbitraryThread:^(id value) {
							 | 
						||
| 
								 | 
							
								            if (!first) registerSelf();
							 | 
						||
| 
								 | 
							
								            first = false;
							 | 
						||
| 
								 | 
							
								            [a addObject:value];
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
								        } untilCancelled:c.token];
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								    };
							 | 
						||
| 
								 | 
							
								    registerSelf = [registerSelf_builder copy];
							 | 
						||
| 
								 | 
							
								    registerSelf();
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    // adding during a callback counts as adding after the callback
							 | 
						||
| 
								 | 
							
								    // so we should see a doubling each time
							 | 
						||
| 
								 | 
							
								    test([a isEqualToArray:@[@""]]);
							 | 
						||
| 
								 | 
							
								    [s updateValue:@1];
							 | 
						||
| 
								 | 
							
								    test([a isEqualToArray:(@[@"", @1, @1])]);
							 | 
						||
| 
								 | 
							
								    [s updateValue:@2];
							 | 
						||
| 
								 | 
							
								    test([a isEqualToArray:(@[@"", @1, @1, @2, @2, @2, @2])]);
							 | 
						||
| 
								 | 
							
								    [s updateValue:@3];
							 | 
						||
| 
								 | 
							
								    test([a isEqualToArray:(@[@"", @1, @1, @2, @2, @2, @2, @3, @3, @3, @3, @3, @3, @3, @3])]);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								-(void) testObservableReentrantRemove {
							 | 
						||
| 
								 | 
							
								    ObservableValueController* s = [ObservableValueController observableValueControllerWithInitialValue:@""];
							 | 
						||
| 
								 | 
							
								    ObservableValue* t = s;
							 | 
						||
| 
								 | 
							
								    NSMutableArray* a = [NSMutableArray array];
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
								    TOCCancelTokenSource* c = [TOCCancelTokenSource new];
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								    
							 | 
						||
| 
								 | 
							
								    for (int i = 0; i < 3; i++) {
							 | 
						||
| 
								 | 
							
								        __block bool first = true;
							 | 
						||
| 
								 | 
							
								        [t watchLatestValueOnArbitraryThread:^(id value) {
							 | 
						||
| 
								 | 
							
								            if (!first) {
							 | 
						||
| 
								 | 
							
								                [c cancel];
							 | 
						||
| 
								 | 
							
								                [a addObject:value];
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            first = false;
							 | 
						||
| 
								 
											11 years ago
										 
									 | 
							
								        } untilCancelled:c.token];
							 | 
						||
| 
								 
											12 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    // removing during a callback counts as removing after the callback
							 | 
						||
| 
								 | 
							
								    // so we should see all the callbacks run, then they're all cancelled
							 | 
						||
| 
								 | 
							
								    test([a isEqualToArray:(@[])]);
							 | 
						||
| 
								 | 
							
								    [s updateValue:@1];
							 | 
						||
| 
								 | 
							
								    test([a isEqualToArray:(@[@1, @1, @1])]);
							 | 
						||
| 
								 | 
							
								    [s updateValue:@2];
							 | 
						||
| 
								 | 
							
								    test([a isEqualToArray:(@[@1, @1, @1])]);
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								@end
							 |