implemented 6 more passing tests for range. Now works like python's range

This commit is contained in:
Kirill Ishanov
2009-12-01 02:10:56 +03:00
parent d8cf99ba89
commit 451d9c5d62
5 changed files with 42 additions and 16 deletions

View File

@@ -2,10 +2,6 @@ $(document).ready(function() {
module("Collection functions (each, any, select, and so on...)");
test("generators: range", function() {
equals(_.range(4).join(' '), '0 1 2 3 4', 'range with positive number generates an array of of elements 0,1,2,...,n');
});
test("collections: each", function() {
_.each([1, 2, 3], function(num, i) {
equals(num, i + 1, 'each iterators provide value and iteration count');

15
test/generators.js Normal file
View File

@@ -0,0 +1,15 @@
$(document).ready(function() {
module("Generator functions (range...)");
test("generators: range", function() {
equals(_.range(0).join(''), '', 'range with 0 as a first argument generates an empty array');
equals(_.range(4).join(' '), '0 1 2 3', 'range with a single positive argument generates an array of elements 0,1,2,...,n-1');
equals(_.range(5, 8).join(' '), '5 6 7', 'range with two arguments a & b, a<b generates an array of elements a,a+1,a+2,...,b-2,b-1');
equals(_.range(8, 5).join(''), '', 'range with two arguments a & b, b<a generates an empty array');
equals(_.range(3, 10, 3).join(' '), '3 6 9', 'range with three arguments a & b & c, c < b-a, a < b generates an array of elements a,a+c,a+2c,...,b - (multiplier of a) < c');
equals(_.range(3, 10, 15).join(''), '3', 'range with three arguments a & b & c, c > b-a, a < b generates an array with a single element, equal to a');
equals(_.range(12, 7, -2).join(' '), '12 10 8', 'range with three arguments a & b & c, a > b, c < 0 generates an array of elements a,a-c,a-2c and ends with the number not less than b');
});
});

View File

@@ -7,6 +7,7 @@
<script type="text/javascript" src="vendor/qunit.js"></script>
<script type="text/javascript" src="vendor/jslitmus.js"></script>
<script type="text/javascript" src="../underscore.js"></script>
<script type="text/javascript" src="generators.js"></script>
<script type="text/javascript" src="collections.js"></script>
<script type="text/javascript" src="arrays.js"></script>
<script type="text/javascript" src="functions.js"></script>
@@ -31,4 +32,4 @@
</h2>
<br />
</body>
</html>
</html>

View File

@@ -36,7 +36,7 @@ $(document).ready(function() {
"flatten", "foldl", "foldr", "forEach", "functions", "head", "identity", "include",
"indexOf", "inject", "intersect", "invoke", "isArray", "isElement", "isEmpty", "isEqual",
"isFunction", "isNumber", "isString", "isUndefined", "keys", "last", "lastIndexOf", "map", "max",
"methods", "min", "pluck", "reduce", "reduceRight", "reject", "rest", "select",
"methods", "min", "pluck", "range", "reduce", "reduceRight", "reject", "rest", "select",
"size", "some", "sortBy", "sortedIndex", "tail", "template", "toArray", "uniq",
"uniqueId", "values", "without", "wrap", "zip"];
ok(_(expected).isEqual(_.methods()), 'provides a sorted list of functions');

View File

@@ -33,19 +33,33 @@
// Current version.
_.VERSION = '0.4.5';
/*------------------------ Generator Functions: ----------------------------*/
_.range = function(upper, lower, step) {
if (!lower) var lower = 0;
if (!step) var step = 1;
/*------------------------ Generator Functions: ---------------------------*/
var result = new Array(((upper - lower) / step) + 1));
for (var i = lower; i <= upper; i += step) {
result[i] = i;
// Generates an Array, containing an arithmetic progressions
// Analog of python's built-in function 'range'
_.range = function(start, stop, step) {
if (!stop) {
var stop = start;
start = 0;
}
return result;
}
if (!step) var step = 1;
var length = Math.ceil((stop - start) / step);
if (length < 0) {
return [];
}
var results = new Array(length);
var resIdx = 0;
for (var i = start; (start <= stop ? stop - i > 0 : i - stop > 0); i += step) {
results[resIdx++] = i;
}
return results;
};
/*------------------------ Collection Functions: ---------------------------*/