Archived
1

Internal Redesign:

- Combined MPExpression and MPMutableExpression
- Abstracted children of MPExpression into MPExpressionElement protocol
- Abstracted most of MPExpressionLayout and MPFunctionLayout into common superclass MPLayout
This commit is contained in:
Kim Wittenburg
2014-08-11 13:57:48 +02:00
parent 740c3fd80a
commit 60760b8b3d
31 changed files with 1222 additions and 1343 deletions

View File

@@ -20,119 +20,119 @@
- (void)testInitialization {
// Test empty expression
MPExpression *testExpression = [[MPExpression alloc] init];
XCTAssertEqual([testExpression numberOfSymbols], 0);
XCTAssertEqual(testExpression.numberOfElements, 0);
// Test expression with string
testExpression = [[MPExpression alloc] initWithString:@"1234+5678"];
XCTAssertEqual([testExpression numberOfSymbols], 1);
XCTAssertEqualObjects([testExpression symbolAtIndex:0], @"1234+5678");
testExpression = [[MPExpression alloc] initWithElement:@"1234+5678"];
XCTAssertEqual(testExpression.numberOfElements, 1);
XCTAssertEqualObjects([testExpression elementAtIndex:0], @"1234+5678");
// Test expression with function
testExpression = [[MPExpression alloc] initWithFunction:[[MPFunction alloc] init]];
XCTAssertEqual([testExpression numberOfSymbols], 1);
XCTAssertEqualObjects([testExpression symbolAtIndex:0], [[MPFunction alloc] init]);
testExpression = [[MPExpression alloc] initWithElement:[[MPFunction alloc] init]];
XCTAssertEqual([testExpression numberOfElements], 1);
XCTAssertEqualObjects([testExpression elementAtIndex:0], [[MPFunction alloc] init]);
testExpression = [[MPExpression alloc] initWithSymbols:@[@"1234", [[MPFunction alloc] init], @"17", [[MPFunction alloc] init]]];
XCTAssertEqual([testExpression numberOfSymbols], 4);
XCTAssertEqualObjects([testExpression symbolAtIndex:2], @"17");
testExpression = [[MPExpression alloc] initWithElements:@[@"1234", [[MPFunction alloc] init], @"17", [[MPFunction alloc] init]]];
XCTAssertEqual([testExpression numberOfElements], 4);
XCTAssertEqualObjects([testExpression elementAtIndex:2], @"17");
// Test expression with subsequent strings
testExpression = [[MPExpression alloc] initWithSymbols:@[@"1234", @"5678"]];
XCTAssertEqual([testExpression numberOfSymbols], 1);
XCTAssertEqualObjects([testExpression symbolAtIndex:0], @"12345678");
testExpression = [[MPExpression alloc] initWithElements:@[@"1234", @"5678"]];
XCTAssertEqual([testExpression numberOfElements], 1);
XCTAssertEqualObjects([testExpression elementAtIndex:0], @"12345678");
// Test expression with only empty string
testExpression = [[MPExpression alloc] initWithString:@""];
XCTAssertEqual([testExpression numberOfSymbols], 0);
testExpression = [[MPExpression alloc] initWithElement:@""];
XCTAssertEqual([testExpression numberOfElements], 0);
}
- (void)testSubexpressions {
MPExpression *testExpression = [MPExpression expressionWithSymbols:@[@"1234", [[MPFunction alloc] init], @"17", [[MPFunction alloc] init]]];
MPExpression *testExpression = [[MPExpression alloc] initWithElements:@[@"1234", [[MPFunction alloc] init], @"17", [[MPFunction alloc] init]]];
/********** subexpressionFromIndex: **********/
// Test with start index at front
MPExpression *subexpression = [testExpression subexpressionFromIndex:0];
XCTAssertEqual([subexpression numberOfSymbols], 4);
XCTAssertEqualObjects([subexpression symbolAtIndex:0], @"1234");
MPExpression *subexpression = [testExpression subexpressionFromLocation:0];
XCTAssertEqual([subexpression numberOfElements], 4);
XCTAssertEqualObjects([subexpression elementAtIndex:0], @"1234");
// Test with start index in first symbol
subexpression = [testExpression subexpressionFromIndex:2];
XCTAssertEqual([subexpression numberOfSymbols], 4);
XCTAssertEqualObjects([subexpression symbolAtIndex:0], @"34");
// Test with start index in first element
subexpression = [testExpression subexpressionFromLocation:2];
XCTAssertEqual([subexpression numberOfElements], 4);
XCTAssertEqualObjects([subexpression elementAtIndex:0], @"34");
// Test with start index in middle symbol starting with a literal
subexpression = [testExpression subexpressionFromIndex:6];
XCTAssertEqual([subexpression numberOfSymbols], 2);
XCTAssertEqualObjects([subexpression symbolAtIndex:0], @"7");
// Test with start index in middle element starting with a literal
subexpression = [testExpression subexpressionFromLocation:6];
XCTAssertEqual([subexpression numberOfElements], 2);
XCTAssertEqualObjects([subexpression elementAtIndex:0], @"7");
// Test with start index in middle symbol starting with a function
subexpression = [testExpression subexpressionFromIndex:4];
XCTAssertEqual([subexpression numberOfSymbols], 3);
XCTAssertEqualObjects([subexpression symbolAtIndex:0], [[MPFunction alloc] init]);
// Test with start index in middle element starting with a function
subexpression = [testExpression subexpressionFromLocation:4];
XCTAssertEqual([subexpression numberOfElements], 3);
XCTAssertEqualObjects([subexpression elementAtIndex:0], [[MPFunction alloc] init]);
// Test with start index in last symbol
subexpression = [testExpression subexpressionFromIndex:7];
XCTAssertEqual([subexpression numberOfSymbols], 1);
XCTAssertEqualObjects([subexpression symbolAtIndex:0], [[MPFunction alloc] init]);
// Test with start index in last element
subexpression = [testExpression subexpressionFromLocation:7];
XCTAssertEqual([subexpression numberOfElements], 1);
XCTAssertEqualObjects([subexpression elementAtIndex:0], [[MPFunction alloc] init]);
// Test with start index at end
subexpression = [testExpression subexpressionFromIndex:8];
XCTAssertEqual([subexpression numberOfSymbols], 0);
subexpression = [testExpression subexpressionFromLocation:8];
XCTAssertEqual([subexpression numberOfElements], 0);
/********** subexpressionToIndex: **********/
// Test with end index at front
subexpression = [testExpression subexpressionToIndex:0];
XCTAssertEqual([subexpression numberOfSymbols], 0);
subexpression = [testExpression subexpressionToLocation:0];
XCTAssertEqual([subexpression numberOfElements], 0);
// Test with end index in first symbol
subexpression = [testExpression subexpressionToIndex:2];
XCTAssertEqual([subexpression numberOfSymbols], 1);
XCTAssertEqualObjects([subexpression symbolAtIndex:0], @"12");
// Test with end index in first Element
subexpression = [testExpression subexpressionToLocation:2];
XCTAssertEqual([subexpression numberOfElements], 1);
XCTAssertEqualObjects([subexpression elementAtIndex:0], @"12");
// Test with end index in middle symbol ending with a literal
subexpression = [testExpression subexpressionToIndex:6];
XCTAssertEqual([subexpression numberOfSymbols], 3);
XCTAssertEqualObjects([subexpression symbolAtIndex:2], @"1");
// Test with end index in middle Element ending with a literal
subexpression = [testExpression subexpressionToLocation:6];
XCTAssertEqual([subexpression numberOfElements], 3);
XCTAssertEqualObjects([subexpression elementAtIndex:2], @"1");
// Test with end index in middle symbol ending with a function
subexpression = [testExpression subexpressionToIndex:5];
XCTAssertEqual([subexpression numberOfSymbols], 2);
XCTAssertEqualObjects([subexpression symbolAtIndex:1], [[MPFunction alloc] init]);
// Test with end index in middle Element ending with a function
subexpression = [testExpression subexpressionToLocation:5];
XCTAssertEqual([subexpression numberOfElements], 2);
XCTAssertEqualObjects([subexpression elementAtIndex:1], [[MPFunction alloc] init]);
// Test with end index at end
subexpression = [testExpression subexpressionToIndex:8];
XCTAssertEqual([subexpression numberOfSymbols], 4);
XCTAssertEqualObjects([subexpression symbolAtIndex:3], [[MPFunction alloc] init]);
subexpression = [testExpression subexpressionToLocation:8];
XCTAssertEqual([subexpression numberOfElements], 4);
XCTAssertEqualObjects([subexpression elementAtIndex:3], [[MPFunction alloc] init]);
/********** subexpressionWithRange: **********/
// Test with empty range
subexpression = [testExpression subexpressionWithRange:NSMakeRange(4, 0)];
XCTAssertEqual([subexpression numberOfSymbols], 0);
XCTAssertEqual([subexpression numberOfElements], 0);
// Test with start and end in first symbol
// Test with start and end in first element
subexpression = [testExpression subexpressionWithRange:NSMakeRange(1, 2)];
XCTAssertEqual([subexpression numberOfSymbols], 1);
XCTAssertEqualObjects([subexpression symbolAtIndex:0], @"23");
XCTAssertEqual([subexpression numberOfElements], 1);
XCTAssertEqualObjects([subexpression elementAtIndex:0], @"23");
// Test with start in first and end in middle after function
subexpression = [testExpression subexpressionWithRange:NSMakeRange(2, 3)];
XCTAssertEqual([subexpression numberOfSymbols], 2);
XCTAssertEqualObjects([subexpression symbolAtIndex:0], @"34");
XCTAssertEqualObjects([subexpression symbolAtIndex:1], [[MPFunction alloc] init]);
XCTAssertEqual([subexpression numberOfElements], 2);
XCTAssertEqualObjects([subexpression elementAtIndex:0], @"34");
XCTAssertEqualObjects([subexpression elementAtIndex:1], [[MPFunction alloc] init]);
// Test with start in first and end in middle after literal
subexpression = [testExpression subexpressionWithRange:NSMakeRange(2, 4)];
XCTAssertEqual([subexpression numberOfSymbols], 3);
XCTAssertEqualObjects([subexpression symbolAtIndex:0], @"34");
XCTAssertEqualObjects([subexpression symbolAtIndex:2], @"1");
XCTAssertEqual([subexpression numberOfElements], 3);
XCTAssertEqualObjects([subexpression elementAtIndex:0], @"34");
XCTAssertEqualObjects([subexpression elementAtIndex:2], @"1");
}
- (void)testSubexpressionsIllegalRange {
MPExpression *testExpression = [MPExpression expressionWithSymbols:@[@"1234", [[MPFunction alloc] init], @"17", [[MPFunction alloc] init]]];
MPExpression *testExpression = [[MPExpression alloc] initWithElements:@[@"1234", [[MPFunction alloc] init], @"17", [[MPFunction alloc] init]]];
// Test with start index beyond end
@try {
[testExpression subexpressionFromIndex:10];
[testExpression subexpressionFromLocation:10];
XCTFail(@"Should have raised an exception.");
}
@catch (NSException *exception) {}
@@ -146,11 +146,11 @@
}
- (void)testEqualExpressions {
MPExpression *expression1 = [[MPExpression alloc] initWithString:@"123"];
MPExpression *expression2 = [[MPExpression alloc] initWithString:@"1234"];
MPExpression *expression3 = [[MPExpression alloc] initWithFunction:[[MPFunction alloc] init]];
MPExpression *expression4 = [[MPExpression alloc] initWithSymbols:@[[[MPFunction alloc] init], @"123"]];
MPExpression *expression5 = [[MPExpression alloc] initWithSymbols:@[[[MPFunction alloc] init], @"123"]];
MPExpression *expression1 = [[MPExpression alloc] initWithElement:@"123"];
MPExpression *expression2 = [[MPExpression alloc] initWithElement:@"1234"];
MPExpression *expression3 = [[MPExpression alloc] initWithElement:[[MPFunction alloc] init]];
MPExpression *expression4 = [[MPExpression alloc] initWithElements:@[[[MPFunction alloc] init], @"123"]];
MPExpression *expression5 = [[MPExpression alloc] initWithElements:@[[[MPFunction alloc] init], @"123"]];
XCTAssertNotEqualObjects(expression1, expression2);
XCTAssertNotEqualObjects(expression1, expression3);
@@ -159,68 +159,83 @@
XCTAssertEqualObjects(expression4, expression5);
}
- (void)testAppendSymbols {
MPExpression *expression1 = [[MPExpression alloc] initWithString:@"123"];
MPExpression *expression2 = [expression1 expressionByAppendingFunction:[[MPFunction alloc] init]];
MPExpression *expression3 = [[MPExpression alloc] initWithSymbols:@[@"123", [[MPFunction alloc] init]]];
XCTAssertEqualObjects(expression2, expression3);
}
- (void)testDescription {
// Test Simple Expressions
MPExpression *testExpression = [[MPExpression alloc] initWithString:@"1234"];
MPExpression *testExpression = [[MPExpression alloc] initWithElement:@"1234"];
XCTAssertEqualObjects([testExpression description], @"1234");
testExpression = [[MPExpression alloc] initWithFunction:[[MPFunction alloc] init]];
testExpression = [[MPExpression alloc] initWithElement:[[MPFunction alloc] init]];
XCTAssertEqualObjects([testExpression description], @"[]");
// Test function after literal without explicit operator
testExpression = [[MPExpression alloc] initWithSymbols:@[@"123", [[MPFunction alloc] init]]];
testExpression = [[MPExpression alloc] initWithElements:@[@"123", [[MPFunction alloc] init]]];
XCTAssertEqualObjects([testExpression description], @"123*[]");
// Test function after literal with explicit operator
testExpression = [[MPExpression alloc] initWithSymbols:@[@"123+", [[MPFunction alloc] init]]];
testExpression = [[MPExpression alloc] initWithElements:@[@"123+", [[MPFunction alloc] init]]];
XCTAssertEqualObjects([testExpression description], @"123+[]");
// Test literal after function without explicit operator
testExpression = [[MPExpression alloc] initWithSymbols:@[[[MPFunction alloc] init], @"123"]];
testExpression = [[MPExpression alloc] initWithElements:@[[[MPFunction alloc] init], @"123"]];
XCTAssertEqualObjects([testExpression description], @"[]*123");
// Test literal after function with explicit operator
testExpression = [[MPExpression alloc] initWithSymbols:@[[[MPFunction alloc] init], @"-123"]];
testExpression = [[MPExpression alloc] initWithElements:@[[[MPFunction alloc] init], @"-123"]];
XCTAssertEqualObjects([testExpression description], @"[]-123");
// Test function after function without explicit operator
testExpression = [[MPExpression alloc] initWithSymbols:@[[[MPFunction alloc] init], [[MPFunction alloc] init]]];
testExpression = [[MPExpression alloc] initWithElements:@[[[MPFunction alloc] init], [[MPFunction alloc] init]]];
XCTAssertEqualObjects([testExpression description], @"[]*[]");
// Test function after function with explicit operator
testExpression = [[MPExpression alloc] initWithSymbols:@[[[MPFunction alloc] init], @"-", [[MPFunction alloc] init]]];
testExpression = [[MPExpression alloc] initWithElements:@[[[MPFunction alloc] init], @"-", [[MPFunction alloc] init]]];
XCTAssertEqualObjects([testExpression description], @"[]-[]");
// Test whitespaces in literal
testExpression = [[MPExpression alloc] initWithSymbols:@[@" 123 + ", [[MPFunction alloc] init]]];
testExpression = [[MPExpression alloc] initWithElements:@[@" 123 + ", [[MPFunction alloc] init]]];
XCTAssertEqualObjects([testExpression description], @"123 +[]");
}
- (void)testCopying {
MPExpression *baseExpression = [[MPExpression alloc] initWithFunction:[[MPFunction alloc] init]];
MPExpression *baseExpression = [[MPExpression alloc] initWithElement:[[MPFunction alloc] init]];
MPExpression *copy = [baseExpression copy];
XCTAssertEqual(baseExpression.numberOfElements, copy.numberOfElements);
XCTAssertNotEqual(copy, baseExpression);
XCTAssertNotEqual([baseExpression symbolAtIndex:0], [copy symbolAtIndex:0]);
XCTAssertNotEqual([baseExpression elementAtIndex:0], [copy elementAtIndex:0]);
XCTAssertEqualObjects(baseExpression, copy);
}
- (void)testMutableCopying {
MPExpression *baseExpression = [[MPExpression alloc] initWithString:@"123"];
MPMutableExpression *expression1 = [baseExpression mutableCopy];
[expression1 appendFunction:[[MPFunction alloc] init]];
MPExpression *expression2 = [[MPExpression alloc] initWithSymbols:@[@"123", [[MPFunction alloc] init]]];
- (void)testMutating {
MPExpression *testExpression = [[MPExpression alloc] initWithElements:@[@"1234", [[MPFunction alloc] init], @"5678", [[MPFunction alloc] init]]];
XCTAssertEqualObjects(expression1, expression2);
XCTAssertNotEqualObjects(baseExpression, expression1);
[testExpression appendElement:@"90"];
XCTAssertEqual([testExpression numberOfElements], 5);
// 1234 [] 5678 [] 90
[testExpression deleteElementsInRange:NSMakeRange(2, 4)];
XCTAssertEqual([testExpression numberOfElements], 3);
// 12678 [] 90
[testExpression insertElement:[[MPFunction alloc] init]
atLocation:2];
XCTAssertEqual([testExpression numberOfElements], 5);
// 12 [] 678 [] 90
[testExpression replaceElementsInRange:NSMakeRange(2, 5)
withElements:@[[[MPFunction alloc] init]]];
XCTAssertEqual([testExpression numberOfElements], 3);
// 12 [] 90
}
- (void)testInvalidMutatingRange {
@try {
MPExpression *testExpression = [[MPExpression alloc] initWithElements:@[@"1234", [[MPFunction alloc] init], @"5678", [[MPFunction alloc] init]]];
[testExpression deleteElementsInRange:NSMakeRange(5, 17)];
XCTFail(@"Should have raised an exception");
}
@catch (NSException *exception) {}
}
// TODO: Test evaluating expressions