dartlang.org有一篇很长的文章,介绍了dart 中单元测试的当前状态。API 文档可以在这里找到。
import 'package:unittest/unittest.dart';
void main() {
test('QuickSort', () =>
expect(QuickSort([5, 4, 3, 2, 1]),
orderedEquals([1, 2, 3, 4, 5]))
);
}
int _Partition(List array, int left, int right, int pivotIndex) {
var pivotValue = array[pivotIndex];
array[pivotIndex] = array[right];
array[right] = pivotValue;
var storeIndex = left;
for (var i = left; i < right; i++) {
if (array[i] < pivotValue) {
var tmp = array[i];
array[i] = array[storeIndex];
array[storeIndex] = tmp;
}
}
var tmp = array[storeIndex];
array[storeIndex] = array[right];
array[right] = tmp;
return storeIndex;
}
void _QuickSort(List array, int left, int right) {
if (left < right) {
int pivotIndex = left + ((right-left) / 2);
pivotIndex = _Partition(array, left, right, pivotIndex);
_QuickSort(array, left, pivotIndex-1);
_QuickSort(array, pivotIndex+1, right);
}
}
List QuickSort(List array) {
_QuickSort(array, 0, array.length-1);
return array;
}