Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/@protobufjs/utf8/tests/index.js
1129 views
1
var tape = require("tape");
2
3
var utf8 = require("..");
4
5
var data = require("fs").readFileSync(require.resolve("./data/utf8.txt")),
6
dataStr = data.toString("utf8");
7
8
tape.test("utf8", function(test) {
9
10
test.test(test.name + " - length", function(test) {
11
test.equal(utf8.length(""), 0, "should return a byte length of zero for an empty string");
12
13
test.equal(utf8.length(dataStr), Buffer.byteLength(dataStr), "should return the same byte length as node buffers");
14
15
test.end();
16
});
17
18
test.test(test.name + " - read", function(test) {
19
var comp = utf8.read([], 0, 0);
20
test.equal(comp, "", "should decode an empty buffer to an empty string");
21
22
comp = utf8.read(data, 0, data.length);
23
test.equal(comp, data.toString("utf8"), "should decode to the same byte data as node buffers");
24
25
var longData = Buffer.concat([data, data, data, data]);
26
comp = utf8.read(longData, 0, longData.length);
27
test.equal(comp, longData.toString("utf8"), "should decode to the same byte data as node buffers (long)");
28
29
var chunkData = new Buffer(data.toString("utf8").substring(0, 8192));
30
comp = utf8.read(chunkData, 0, chunkData.length);
31
test.equal(comp, chunkData.toString("utf8"), "should decode to the same byte data as node buffers (chunk size)");
32
33
test.end();
34
});
35
36
test.test(test.name + " - write", function(test) {
37
var buf = new Buffer(0);
38
test.equal(utf8.write("", buf, 0), 0, "should encode an empty string to an empty buffer");
39
40
var len = utf8.length(dataStr);
41
buf = new Buffer(len);
42
test.equal(utf8.write(dataStr, buf, 0), len, "should encode to exactly " + len + " bytes");
43
44
test.equal(buf.length, data.length, "should encode to a buffer length equal to that of node buffers");
45
46
for (var i = 0; i < buf.length; ++i) {
47
if (buf[i] !== data[i]) {
48
test.fail("should encode to the same buffer data as node buffers (offset " + i + ")");
49
return;
50
}
51
}
52
test.pass("should encode to the same buffer data as node buffers");
53
54
test.end();
55
});
56
57
});
58
59