patternMinor
Unit Testing Bejeweled Wilds
Viewed 0 times
bejeweledwildstestingunit
Problem
I created a pretty complex algorithm for calculating matches on a Bejeweled board that kept track of matches involving wilds. After doing some in-game testing, I found quite a few bugs that needed to be eliminated.
In order to deal with this problem, I first created an undo system for the game, and then when I saw unexpected behavior during a match, I stepped the game back and made a note of the sequence of orbs that caused the problem. Then I wrote unit tests for those sequences of orbs and refined the algorithm until all of the tests passed. I am now quite sure that there are none or very few bugs.
The match requirements:
I have heard that unit tests are more about writing them than reading them, but I will let you decide:
DMBoardEvalTests.m
```
#import
#import "DMBoardEval.h"
#import "DMOrb.h"
@interface DMBoardEval (Testing)
-(NSMutableArray ) matchesForOrbs:(NSMutableArray )orbs;
@end
@interface DMBoardEvalTests : XCTestCase
@end
@implementation DMBoardEvalTests {
DMBoardEval *_boardEval;
}
[super setUp];
_boardEval = [[DMBoardEval alloc]init];
}
#pragma mark - First Bugs Found
-(void) testBugWithTwoMatchesWhenShouldBeOne {
//originally this returned 2 matches because of problems with the wild search algorithm
NSMutableArray *customArray = [[NSMutableArray alloc]init];
[customArray addObject:[[DMOrb alloc]initWithType:DMOrbTypeYellow]];
[customArray addObject:[[DMOrb alloc]initWithType:DMOrbTypeYellow]];
[customArray addObject:[[DMOrb alloc]initWithType:DMOrbTypeYellow]];
[customArray addObject:[[DMOrb alloc]initWithType:DMOrbTypeBomb]];
[customArray addObject:[[DMOrb alloc]initWithType:DMOrbTypeBrown]];
[customArray addObject:[[DMOrb alloc]initWithType:DMOrbTypeGreen]];
[customArray addObject:[[DMOrb alloc]ini
In order to deal with this problem, I first created an undo system for the game, and then when I saw unexpected behavior during a match, I stepped the game back and made a note of the sequence of orbs that caused the problem. Then I wrote unit tests for those sequences of orbs and refined the algorithm until all of the tests passed. I am now quite sure that there are none or very few bugs.
The match requirements:
- Three or more consecutive orbs of the same type is a match.
- Wilds match with any color including other wilds and can be part of more than one match at a time.
I have heard that unit tests are more about writing them than reading them, but I will let you decide:
DMBoardEvalTests.m
```
#import
#import "DMBoardEval.h"
#import "DMOrb.h"
@interface DMBoardEval (Testing)
-(NSMutableArray ) matchesForOrbs:(NSMutableArray )orbs;
@end
@interface DMBoardEvalTests : XCTestCase
@end
@implementation DMBoardEvalTests {
DMBoardEval *_boardEval;
}
- (void)setUp {
[super setUp];
_boardEval = [[DMBoardEval alloc]init];
}
#pragma mark - First Bugs Found
-(void) testBugWithTwoMatchesWhenShouldBeOne {
//originally this returned 2 matches because of problems with the wild search algorithm
NSMutableArray *customArray = [[NSMutableArray alloc]init];
[customArray addObject:[[DMOrb alloc]initWithType:DMOrbTypeYellow]];
[customArray addObject:[[DMOrb alloc]initWithType:DMOrbTypeYellow]];
[customArray addObject:[[DMOrb alloc]initWithType:DMOrbTypeYellow]];
[customArray addObject:[[DMOrb alloc]initWithType:DMOrbTypeBomb]];
[customArray addObject:[[DMOrb alloc]initWithType:DMOrbTypeBrown]];
[customArray addObject:[[DMOrb alloc]initWithType:DMOrbTypeGreen]];
[customArray addObject:[[DMOrb alloc]ini
Solution
After scrolling through a whole lot of this:
I've decided that you need to add the following method to your
And that applies, regardless of anything else.
But as thorough as it looks like your testing is... isn't it always going to be incomplete unless you just write a method that iterates through every possible combination of orbs and tests that?
Come up with an algorithm to test every single combination of orbs. That's your unit test. Anything else seems rather incomplete.
I've decided that you need to add the following method to your
DMOrb class:+ (instancetype)orbWithType:(DMOrbType)type {
return [[self alloc] initWithType:type];
}And that applies, regardless of anything else.
But as thorough as it looks like your testing is... isn't it always going to be incomplete unless you just write a method that iterates through every possible combination of orbs and tests that?
Come up with an algorithm to test every single combination of orbs. That's your unit test. Anything else seems rather incomplete.
Code Snippets
+ (instancetype)orbWithType:(DMOrbType)type {
return [[self alloc] initWithType:type];
}Context
StackExchange Code Review Q#78245, answer score: 5
Revisions (0)
No revisions yet.