Skip to content

Instantly share code, notes, and snippets.

@zolotyh
Forked from NevRA/exampleSort.dart
Last active December 1, 2018 10:05
Show Gist options
  • Save zolotyh/bd22d967f8a8082bd739b1bdfb4b56b9 to your computer and use it in GitHub Desktop.
Save zolotyh/bd22d967f8a8082bd739b1bdfb4b56b9 to your computer and use it in GitHub Desktop.
// Сортировка по образцу
// Напиши функцию, которая сортирует массив по каталогу. В каталоге есть все элементы из входного массива, но во входном массиве необязательно есть все элементы из каталога.
// Пример:
// Входной массив: [1,3,4,4,4,4,5]
// Каталог: [4,1,2,3,5]
// Результат: [4,4,4,4,1,3,5]
Iterable<int> exampleSort(Iterable<int> array, Iterable<int> catalog) {
return [1, 2, 3];
}
bool hasErrors = false;
List<String> messages = [];
void main() {
test('should work with empty array and catalog', () {
expect(exampleSort([], []), equals([]));
});
test('should work with empty array', () {
expect(exampleSort([], [1, 2, 3]), equals(<int>[]));
});
test('should work with example', () {
expect(exampleSort([1, 3, 4, 4, 4, 4, 5], [4, 1, 2, 3, 5]), equals([4, 4, 4, 4, 1, 3, 5]));
});
test('should work with different array size', () {
expect(exampleSort([1, 2, 0, 3], [0, 1]), equals([0, 1]));
expect(exampleSort([1, 2, 0, 3], [3]), equals([3]));
});
test('should work with equal array size', () {
expect(exampleSort([1, 2, 3, 4, 5], [2, 3, 4, 1, 5]), equals([2, 3, 4, 1, 5]));
});
test('should work with repeated values', () {
expect(exampleSort([1, 2, 3, 3, 3, 4, 5], [2, 3, 4, 1, 5]), equals([2, 3, 3, 3, 4, 1, 5]));
expect(exampleSort([1, 2, 3, 3, 3, 5], [2, 3, 4, 1, 5]), equals([2, 3, 3, 3, 1, 5]));
expect(exampleSort([1, 2, 3, 3, 3, 5], [3, 4, 5, 6, 9, 11, 12, 13, 1, 7, 8, 2, 10]), equals([3, 3, 3, 5, 1, 2]));
expect(exampleSort([1, 1, 1, 5, 1, 7], [1, 7, 4, 5]), equals([1, 1, 1, 1, 7, 5]));
});
if(!hasErrors ){
print('💪Tests are passed! \n\n');
} else {
print('💩 Tests aren\'t passed! \n\n');
}
print(messages.join('\n'));
}
typedef bool Checker(dynamic input);
Checker equals(dynamic input) {
return (dynamic internalInput) {
input.toString() == internalInput.toString()
? true
: throw AssertionError('value: $input is not equal: $internalInput');
};
}
void test(String name, Function input) {
try {
input();
messages.add('✓   $name');
} catch (e) {
hasErrors = true;
if (e is AssertionError) {
messages.add('✗   $test failed \n      name: $name\n      exception: ${e.message}');
}
}
}
void expect(dynamic input, bool validator(dynamic validatorInput)) {
validator(input);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment