length
This commit is contained in:
@@ -17,12 +17,12 @@ function assertEqual(actual, expected, message) {
|
||||
return {
|
||||
test_create_empty_blob: function() {
|
||||
var b = Blob();
|
||||
assertEqual(b.length, 0, "Empty blob should have length 0");
|
||||
assertEqual(length(b), 0, "Empty blob should have length 0");
|
||||
},
|
||||
|
||||
test_create_blob_with_capacity: function() {
|
||||
var b = Blob(100);
|
||||
assertEqual(b.length, 0, "New blob with capacity should still have length 0");
|
||||
assertEqual(length(b), 0, "New blob with capacity should still have length 0");
|
||||
},
|
||||
|
||||
test_write_and_read_single_bit: function() {
|
||||
@@ -31,7 +31,7 @@ return {
|
||||
b.write_bit(false);
|
||||
b.write_bit(1);
|
||||
b.write_bit(0);
|
||||
assertEqual(b.length, 4, "Should have 4 bits after writing");
|
||||
assertEqual(length(b), 4, "Should have 4 bits after writing");
|
||||
|
||||
stone(b);
|
||||
assertEqual(b.read_logical(0), true, "First bit should be true");
|
||||
@@ -95,7 +95,7 @@ return {
|
||||
var b2 = Blob(10);
|
||||
b2.write_blob(b1);
|
||||
b2.write_bit(false);
|
||||
assertEqual(b2.length, 4, "Combined blob should have 4 bits");
|
||||
assertEqual(length(b2), 4, "Combined blob should have 4 bits");
|
||||
|
||||
stone(b2);
|
||||
assertEqual(b2.read_logical(0), true);
|
||||
@@ -114,7 +114,7 @@ return {
|
||||
|
||||
var b2 = Blob(b1);
|
||||
stone(b2);
|
||||
assertEqual(b2.length, 4, "Copied blob should have same length");
|
||||
assertEqual(length(b2), 4, "Copied blob should have same length");
|
||||
assertEqual(b2.read_logical(0), true);
|
||||
assertEqual(b2.read_logical(3), true);
|
||||
},
|
||||
@@ -128,7 +128,7 @@ return {
|
||||
|
||||
var b2 = Blob(b1, 2, 7);
|
||||
stone(b2);
|
||||
assertEqual(b2.length, 5, "Partial copy should have 5 bits");
|
||||
assertEqual(length(b2), 5, "Partial copy should have 5 bits");
|
||||
assertEqual(b2.read_logical(0), true);
|
||||
assertEqual(b2.read_logical(2), true);
|
||||
},
|
||||
@@ -167,7 +167,7 @@ return {
|
||||
b.write_bit(true);
|
||||
b.write_pad(8);
|
||||
|
||||
assertEqual(b.length, 8, "Should be padded to 8 bits");
|
||||
assertEqual(length(b), 8, "Should be padded to 8 bits");
|
||||
stone(b);
|
||||
|
||||
assert(b['pad?'](3, 8), "Should detect valid padding at position 3");
|
||||
@@ -183,7 +183,7 @@ return {
|
||||
|
||||
var b2 = b1.read_blob(4, 12);
|
||||
stone(b2);
|
||||
assertEqual(b2.length, 8, "Read blob should have 8 bits");
|
||||
assertEqual(length(b2), 8, "Read blob should have 8 bits");
|
||||
|
||||
assertEqual(b2.read_logical(2), true);
|
||||
assertEqual(b2.read_logical(5), true);
|
||||
@@ -248,20 +248,20 @@ return {
|
||||
b.write_bit(false);
|
||||
b.write_number(-999.999);
|
||||
|
||||
var originalLength = b.length;
|
||||
var originalLength = length(b);
|
||||
stone(b);
|
||||
|
||||
var b2 = Blob(b);
|
||||
stone(b2);
|
||||
assertEqual(b2.length, originalLength, "Copy should have same length");
|
||||
assertEqual(length(b2), originalLength, "Copy should have same length");
|
||||
assertEqual(b2.read_text(0), "Test", "First text should match");
|
||||
},
|
||||
|
||||
test_zero_capacity_blob: function() {
|
||||
var b = Blob(0);
|
||||
assertEqual(b.length, 0, "Zero capacity blob should have length 0");
|
||||
assertEqual(length(b), 0, "Zero capacity blob should have length 0");
|
||||
b.write_bit(true);
|
||||
assertEqual(b.length, 1, "Should expand when writing");
|
||||
assertEqual(length(b), 1, "Should expand when writing");
|
||||
},
|
||||
|
||||
test_large_blob_handling: function() {
|
||||
@@ -272,7 +272,7 @@ return {
|
||||
b.write_bit(i % 7 == 0);
|
||||
}
|
||||
|
||||
assertEqual(b.length, testSize, "Should have " + testSize + " bits");
|
||||
assertEqual(length(b), testSize, "Should have " + testSize + " bits");
|
||||
stone(b);
|
||||
|
||||
assertEqual(b.read_logical(0), true, "Bit 0 should be true");
|
||||
|
||||
@@ -20,11 +20,7 @@ return {
|
||||
while(true) {
|
||||
var chunk = fd.read(f2, chunksize);
|
||||
data.write_blob(chunk);
|
||||
// chunk.length is in bits, chunksize is bytes?
|
||||
// fd.read usually takes bytes. Blob.length is bits.
|
||||
// If chunk is blob, length is bits.
|
||||
// fd.read returns blob.
|
||||
if (chunk.length < chunksize * 8) break;
|
||||
if (length(chunk) < chunksize * 8) break;
|
||||
}
|
||||
fd.close(f2)
|
||||
log.console(`read took ${time.number()-st}`)
|
||||
|
||||
@@ -12,6 +12,6 @@ return {
|
||||
st = time.number()-st
|
||||
log.console(`took ${btime*1000000} us to make blob; took ${st*1000000} us to make it text`)
|
||||
log.console(lower(guid))
|
||||
log.console(guid.length)
|
||||
log.console(length(guid))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,5 +3,5 @@ var http = use('http')
|
||||
return function() {
|
||||
var url = "http://example.com"
|
||||
var b2 = http.fetch(url)
|
||||
if (b2.length == 0) throw "Empty response"
|
||||
if (length(b2) == 0) throw "Empty response"
|
||||
}
|
||||
@@ -54,9 +54,9 @@ return {
|
||||
var reader = miniz.read(zip_blob)
|
||||
|
||||
var listed = reader.list()
|
||||
if (listed.length != reader.count())
|
||||
if (length(listed) != reader.count())
|
||||
throw "list/count mismatch"
|
||||
if (listed.length != 2)
|
||||
if (length(listed) != 2)
|
||||
throw "unexpected entry count"
|
||||
} finally {
|
||||
try { fd.unlink(ZIP_PATH) } catch(e) {}
|
||||
|
||||
@@ -8,7 +8,7 @@ function stone_if_needed(b) { if (!stone.p(b)) stone(b) }
|
||||
|
||||
function bytes_to_blob(bytes) {
|
||||
var b = blob()
|
||||
for (var i = 0; i < bytes.length; i++) {
|
||||
for (var i = 0; i < length(bytes); i++) {
|
||||
var byte = bytes[i]
|
||||
for (var bit = 7; bit >= 0; bit--) b.write_bit((byte >> bit) & 1)
|
||||
}
|
||||
@@ -40,7 +40,7 @@ function deepCompare(expected, actual, path) {
|
||||
if (is_blob(expected) && is_blob(actual)) {
|
||||
stone_if_needed(expected); stone_if_needed(actual)
|
||||
if (expected.length != actual.length)
|
||||
return { passed: false, messages: [`blob length mismatch at ${path}: ${expected.length} vs ${actual.length}`] }
|
||||
return { passed: false, messages: [`blob length mismatch at ${path}: ${length(expected)} vs ${length(actual)}`] }
|
||||
for (var i = 0; i < expected.length; i++) {
|
||||
if (expected.read_logical(i) != actual.read_logical(i))
|
||||
return { passed: false, messages: [`blob bit mismatch at ${path}[${i}]`] }
|
||||
@@ -52,7 +52,7 @@ function deepCompare(expected, actual, path) {
|
||||
if (expected.length != actual.length)
|
||||
return {
|
||||
passed: false,
|
||||
messages: [`Array length mismatch at ${path}: expected ${expected.length}, got ${actual.length}`]
|
||||
messages: [`Array length mismatch at ${path}: expected ${length(expected)}, got ${length(actual)}`]
|
||||
};
|
||||
var messages = [];
|
||||
for (var i = 0; i < expected.length; i++) {
|
||||
@@ -174,7 +174,7 @@ var testCases = [
|
||||
];
|
||||
|
||||
var tests = {};
|
||||
for (var i = 0; i < testCases.length; i++) {
|
||||
for (var i = 0; i < length(testCases); i++) {
|
||||
var t = testCases[i];
|
||||
tests[t.name] = makeTest(t);
|
||||
}
|
||||
|
||||
@@ -1905,7 +1905,7 @@ return {
|
||||
var fn = function(a, b) { return a + b }
|
||||
var caught = false
|
||||
try {
|
||||
var x = fn.length
|
||||
var x = length(fn)
|
||||
} catch (e) {
|
||||
caught = true
|
||||
}
|
||||
@@ -3543,7 +3543,7 @@ return {
|
||||
test_text_split_text: function() {
|
||||
var text = "hello world"
|
||||
var result = array(text, " ")
|
||||
if (result.length != 2) throw "text split failed"
|
||||
if (length(result) != 2) throw "text split failed"
|
||||
if (result[0] != "hello") throw "text split failed"
|
||||
if (result[1] != "world") throw "text split failed"
|
||||
},
|
||||
@@ -3551,7 +3551,7 @@ return {
|
||||
test_text_split_regex: function() {
|
||||
var text = "hello world"
|
||||
var result = array(text, /\s+/)
|
||||
if (result.length != 2) throw "text split failed"
|
||||
if (length(result) != 2) throw "text split failed"
|
||||
if (result[0] != "hello") throw "text split failed"
|
||||
if (result[1] != "world") throw "text split failed"
|
||||
},
|
||||
|
||||
@@ -8,9 +8,9 @@ function deep_equal(a, b) {
|
||||
if (is_object(a)) {
|
||||
var keys_a = array(a)
|
||||
var keys_b = array(b)
|
||||
if (keys_a.length != keys_b.length) return false
|
||||
if (length(keys_a) != length(keys_b)) return false
|
||||
|
||||
for (var i = 0; i < keys_a.length; i++) {
|
||||
for (var i = 0; i < length(keys_a); i++) {
|
||||
if (!deep_equal(a[keys_a[i]], b[keys_a[i]])) return false
|
||||
}
|
||||
return true
|
||||
|
||||
@@ -21,9 +21,9 @@ function deep_compare(expected, actual, path) {
|
||||
|
||||
if (is_blob(expected) && is_blob(actual)) {
|
||||
stone_if_needed(expected); stone_if_needed(actual)
|
||||
if (expected.length != actual.length)
|
||||
return { passed: false, messages: [`blob length mismatch at ${path}: ${expected.length} vs ${actual.length}`] }
|
||||
for (var i = 0; i < expected.length; i++) {
|
||||
if (length(expected) != length(actual))
|
||||
return { passed: false, messages: [`blob length mismatch at ${path}: ${length(expected)} vs ${actual)}`] }
|
||||
for (var i = 0; i < length(expected); i++) {
|
||||
if (expected.read_logical(i) != actual.read_logical(i))
|
||||
return { passed: false, messages: [`blob bit mismatch at ${path}[${i}]`] }
|
||||
}
|
||||
@@ -31,16 +31,16 @@ function deep_compare(expected, actual, path) {
|
||||
}
|
||||
|
||||
if (is_array(expected) && is_array(actual)) {
|
||||
if (expected.length != actual.length)
|
||||
return { passed: false, messages: [`Array length mismatch at ${path}: ${expected.length} vs ${actual.length}`] }
|
||||
if (length(expected) != length(actual))
|
||||
return { passed: false, messages: [`Array length mismatch at ${path}: ${length(expected)} vs ${length(actual)}`] }
|
||||
var msgs = []
|
||||
for (var i = 0; i < expected.length; i++) {
|
||||
for (var i = 0; i < length(expected); i++) {
|
||||
var res = deep_compare(expected[i], actual[i], `${path}[${i}]`)
|
||||
if (!res.passed) {
|
||||
for(var m of res.messages) msgs.push(m)
|
||||
}
|
||||
}
|
||||
return { passed: msgs.length == 0, messages: msgs }
|
||||
return { passed: length(msgs) == 0, messages: msgs }
|
||||
}
|
||||
|
||||
if (is_object(expected) && is_object(actual)) {
|
||||
@@ -55,7 +55,7 @@ function deep_compare(expected, actual, path) {
|
||||
for(var m of res.messages) msgs.push(m)
|
||||
}
|
||||
}
|
||||
return { passed: msgs.length == 0, messages: msgs }
|
||||
return { passed: length(msgs) == 0, messages: msgs }
|
||||
}
|
||||
|
||||
return { passed: false, messages: [`Value mismatch at ${path}: ${JSON.stringify(expected)} vs ${JSON.stringify(actual)}`] }
|
||||
@@ -120,7 +120,7 @@ function make_test(t) {
|
||||
}
|
||||
|
||||
var tests = {}
|
||||
for (var i = 0; i < testCases.length; i++) {
|
||||
for (var i = 0; i < length(testCases); i++) {
|
||||
var t = testCases[i]
|
||||
var name = t.name || ('case_' + i)
|
||||
tests[name] = make_test(t)
|
||||
|
||||
Reference in New Issue
Block a user