Last active
December 30, 2015 16:58
-
-
Save davidB/7857605 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:propcheck/propcheck.dart'; | |
import 'package:enumerators/combinators.dart' as c; | |
import 'package:enumerators/enumerators.dart' as e; | |
import 'package:unittest/unittest.dart'; | |
//import 'package:unittest/compact_vm_config.dart'; | |
import 'package:unittest/vm_config.dart'; | |
class Name { | |
String first; | |
String last; | |
Name(this.first, this.last); | |
equals(x) { | |
return x != null && first == x.first && last == x.last; | |
} | |
toString(){ | |
return first + ' ' +last; | |
} | |
} | |
class Santa { | |
Name santa; | |
Name target; | |
toString(){ | |
return "(santa : $santa, target: $target)"; | |
} | |
} | |
stringToNameList(str) { | |
return str | |
.split('\n') | |
.map((x)=> x.trim()) | |
.where((x) => x.length > 3) | |
.map((x){ | |
var s = x.split((' ')); | |
return new Name(s[0], s[1]); | |
}) | |
.toList(growable: false) | |
; | |
} | |
typedef List<Santa> SantaGenerator(List<Name> names); | |
SantaGenerator makeSantaGenerator0() { | |
return (List<Name> names) => []; | |
} | |
SantaGenerator makeSantaGenerator1(num shuff) { | |
//TODO | |
} | |
main() { | |
//useCompactVMConfiguration(); | |
useVMConfiguration(); | |
//runTest('makeSantaGenerator0', makeSantaGenerator0()); | |
runTest('makeSantaGenerator1', makeSantaGenerator1(5)); | |
} | |
runTest(String label, SantaGenerator sut) { | |
bool moreThanOneLastName(List<Name> names) { | |
var out = names.length > 1; | |
for(var i = 1; i < names.length && out; i++) { | |
out = out && names[i].last != names[i-1].last; | |
} | |
return out; | |
} | |
/* --- the properties to test --- */ | |
bool santaAndTargetHaveNotSameLastName(List<Name> names) { | |
var santas = sut(names); | |
if (names.length < 2) return true; | |
return santas.fold(true, (acc, v){ | |
return acc && (v.target.last != v.santa.last); | |
}); | |
} | |
bool sameLengthForSantasAndNames(List<Name> names) { | |
//print("names : '$names'"); | |
var santas = sut(names); | |
if (names.length < 2) return santas.length == 0; | |
if (!moreThanOneLastName(names)) return true; | |
var out = names.length == santas.length; | |
if (!out) print("failed lg: ${names} // ${santas}"); | |
return out; | |
} | |
bool greaterThanZeroSanta(List<Name> names) { | |
//print("names : '$names'"); | |
var santas = sut(names); | |
if (names.length < 2) return santas.length == 0; | |
if (!moreThanOneLastName(names)) return true; | |
return names.fold(true, (acc, v){ | |
return acc && (santas.where((x) => v.equals(x.target)).length > 0); | |
}); | |
} | |
bool equalsAsSantaAsTarget(List<Name> names) { | |
var santas = sut(names); | |
inc(m, k, v) { | |
var v0 = m[k]; | |
if (v0 == null) { | |
m[k] = v; | |
} else { | |
m[k] = v + v0; | |
} | |
} | |
var countOfSantaMinusTarget = santas.fold({}, (acc, x){ | |
inc(acc, x.santa, 1); | |
inc(acc, x.target, -1); | |
return acc; | |
}); | |
var out = true; | |
countOfSantaMinusTarget.forEach((k,v){ | |
if (v != 0) { | |
print("failed for name ${k} : ${v} for ${santas} // ${names}"); | |
out = false; | |
} | |
}); | |
return out; | |
} | |
testsOnList(names) { | |
expect(sameLengthForSantasAndNames(names), equals(true), reason: 'sameLengthForSantasAndNames'); | |
expect(greaterThanZeroSanta(names), equals(true), reason: 'greaterThanZeroSanta'); | |
expect(santaAndTargetHaveNotSameLastName(names), equals(true), reason: 'santaAndTargetHaveNotSameLastName'); | |
expect(equalsAsSantaAsTarget(names), equals(true), reason:'equalsAsSantaAsTarget'); | |
} | |
group(label,(){ | |
group(('unit test with hardcoded sample data'), () { | |
test('sample from g+', (){ | |
var names = stringToNameList( | |
""" | |
Zoe Washburne | |
Hoban Washburne | |
Malcolm Reynolds | |
Simon Tam | |
River Tam | |
Buffy Summers | |
Dawn Summers | |
""" | |
); | |
//print("simple : ${names} // ${sut(names)}"); | |
testsOnList(names); | |
}); | |
test('empty list', (){ | |
testsOnList([]); | |
}); | |
test('empty list with every body has same last name', (){ | |
var names = stringToNameList( | |
""" | |
Simon Summers | |
River Summers | |
Buffy Summers | |
Dawn Summers | |
""" | |
); | |
testsOnList(names); | |
}); | |
test('empty list with only one person with a different last name', (){ | |
var names = stringToNameList( | |
""" | |
Simon Tam | |
River Summers | |
Buffy Summers | |
Dawn Summers | |
""" | |
); | |
testsOnList(names); | |
}); | |
}); | |
group('quickcheck', () { | |
//var firstEnum = c.strings; | |
var stringsToEnum = (l) => l.map((x) => e.singleton(x)).reduce((x,y) => x + y).pay(); | |
var firstEnum = stringsToEnum(['Zoe', 'Hoban', 'Malcolm', 'Simon', 'River', 'Buffy', 'Dawn']); | |
var lastEnum = stringsToEnum(['Washburne', 'Reynolds', 'Tam', 'Simon', 'Summers']); | |
var newName = (first) => (last) => new Name(first, last); | |
var nameEnum = e.singleton(newName).apply(firstEnum).apply(lastEnum); | |
var namesEnum = c.listsOf(nameEnum); | |
final qc = new QuickCheck(maxSize: 30, seed: 42); | |
test('santaAndTargetHaveNotSameLastName', () => qc.check(forall(namesEnum, santaAndTargetHaveNotSameLastName))); | |
test('greaterThanZeroSanta', () => qc.check(forall(namesEnum, greaterThanZeroSanta))); | |
test('equalsAsSantaAsTarget', () => qc.check(forall(namesEnum, equalsAsSantaAsTarget))); | |
test('sameLengthForSantasAndNames', () => qc.check(forall(namesEnum, sameLengthForSantasAndNames))); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment