Skip to content

Commit

Permalink
Implement binom.
Browse files Browse the repository at this point in the history
  • Loading branch information
kostub committed Jul 16, 2016
1 parent c424990 commit ecdded7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions iosMath/lib/MTMathListBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ - (MTMathAtom*) atomForCommand:(NSString*) command
frac.numerator = [self buildInternal:true];
frac.denominator = [self buildInternal:true];
return frac;
} else if ([command isEqualToString:@"binom"]) {
// A binom command has 2 arguments
MTFraction* frac = [[MTFraction alloc] initWithRule:NO];
frac.numerator = [self buildInternal:true];
frac.denominator = [self buildInternal:true];
frac.leftDelimiter = @"(";
frac.rightDelimiter = @")";
return frac;
} else if ([command isEqualToString:@"sqrt"]) {
// A sqrt command with one argument
MTRadical* rad = [MTRadical new];
Expand Down
35 changes: 35 additions & 0 deletions iosMathTests/MTMathListBuilderTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,41 @@ - (void) testBrace
XCTAssertEqualObjects(latex, @"{n \\brace k}", @"%@", desc);
}

- (void) testBinom
{
NSString *str = @"\\binom{n}{k}";
MTMathList* list = [MTMathListBuilder buildFromString:str];
NSString* desc = [NSString stringWithFormat:@"Error for string:%@", str];

XCTAssertNotNil(list, @"%@", desc);
XCTAssertEqualObjects(@(list.atoms.count), @1, @"%@", desc);
MTFraction* frac = list.atoms[0];
XCTAssertEqual(frac.type, kMTMathAtomFraction, @"%@", desc);
XCTAssertEqualObjects(frac.nucleus, @"", @"%@", desc);
XCTAssertFalse(frac.hasRule);
XCTAssertEqualObjects(frac.rightDelimiter, @")");
XCTAssertEqualObjects(frac.leftDelimiter, @"(");

MTMathList *subList = frac.numerator;
XCTAssertNotNil(subList, @"%@", desc);
XCTAssertEqualObjects(@(subList.atoms.count), @1, @"%@", desc);
MTMathAtom *atom = subList.atoms[0];
XCTAssertEqual(atom.type, kMTMathAtomVariable, @"%@", desc);
XCTAssertEqualObjects(atom.nucleus, @"n", @"%@", desc);

atom = list.atoms[0];
subList = frac.denominator;
XCTAssertNotNil(subList, @"%@", desc);
XCTAssertEqualObjects(@(subList.atoms.count), @1, @"%@", desc);
atom = subList.atoms[0];
XCTAssertEqual(atom.type, kMTMathAtomVariable, @"%@", desc);
XCTAssertEqualObjects(atom.nucleus, @"k", @"%@", desc);

// convert it back to latex (binom converts to choose)
NSString* latex = [MTMathListBuilder mathListToString:list];
XCTAssertEqualObjects(latex, @"{n \\choose k}", @"%@", desc);
}

static NSArray* getTestDataParseErrors() {
return @[
@[@"}a", @(MTParseErrorMismatchBraces)],
Expand Down

0 comments on commit ecdded7

Please sign in to comment.