dartlang.org有大約current state of unit testing in dart很長的文章。 API文檔可以在here找到。
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;
}
由於unittest包現在可用,您是否可以將接受的答案更改爲以下的Antony。 –