-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
143 lines (125 loc) · 3.87 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var assert = require('assert'),
ByteVector = require('./index');
describe('ByteVector', function () {
describe('Default constructed', function() {
var vector = new ByteVector();
it('default size should be zero', function () {
assert.equal(vector.length, 0);
});
it('#push_back', function () {
var test = [1, 2, 3, 4, 5];
test.forEach(function (val) { vector.push_back(val); });
assert.equal(vector.length, test.length);
assert.equal(vector.back(), test[test.length -1 ]);
});
it('#push_front', function () {
var len = vector.length;
vector.push_front(0);
assert.equal(vector.length, len+1);
assert.equal(vector.front(), 0);
});
it('#pop_front', function () {
var len = vector.length,
front_val = vector.front();
assert.equal(vector.pop_front(), front_val);
assert.equal(vector.length, len - 1);
assert.notEqual(vector.front(), front_val);
});
it('#pop_back', function () {
var len = vector.length,
back_val = vector.back();
assert.equal(vector.pop_back(), back_val);
assert.equal(vector.length, len - 1);
assert.notEqual(vector.back(), back_val);
});
it('#resize', function () {
var len = vector.length,
front_val = vector.front(),
back_val = vector.back();
assert.equal(vector.length <= vector.capacity(), true);
var newSize = vector.size() + 1;
vector.resize(newSize);
assert.equal(vector.length, newSize);
assert.equal(vector.front(), front_val);
assert.notEqual(vector.back(), back_val);
});
it('#get', function () {
assert.equal(vector.get(0), vector.front());
assert.equal(vector.get(vector.length - 1), vector.back());
});
it('#set', function () {
assert.equal(vector.get(0), vector.front());
vector.set(0, 100);
assert.equal(vector.front(), 100);
});
it('#assign', function () {
var list = [5];
vector.assign(list);
assert.equal(vector.front(), list[0]);
assert.equal(vector.back(), list[list.length-1]);
assert.equal(vector.length, list.length);
while (list.length) {
assert.equal(list.pop(), vector.pop());
}
});
it('#forEach', function () {
var counter = 0;
vector.forEach(function(value, index, array) {
assert.equal(vector, array);
assert.equal(value, vector.get(counter));
assert(counter++, index);
});
assert.equal(counter, vector.length);
});
it('#clear', function () {
vector.clear();
assert.equal(vector.size(), 0);
});
it('#shrink_to_fit', function () {
vector.push_back(1);
vector.push_back(2);
vector.push_back(3);
vector.pop_back();
vector.shrink_to_fit();
assert.equal(vector.size(), vector.capacity());
assert.equal(vector.get(0), 1);
assert.equal(vector.get(1), 2);
});
it('#buffer', function () {
var buf = vector.buffer();
assert.equal(buf instanceof Uint8Array, true);
});
var shouldImplement = [
'front',
'back',
'push',
'pop',
'reserve',
'capacity',
'resize',
'push_back',
'push_front',
'pop_back',
'pop_front',
'get',
'set',
'clear',
'buffer',
'shrink_to_fit'
];
shouldImplement.forEach(function (method) {
it('should implement method: ' + method, function () {
assert.equal(typeof vector[method], 'function');
});
});
});
describe('Array constructed', function () {
var vector = new ByteVector([1, 2, 3, 4, 5]);
vector.push_back(6);
vector.pop_front();
var copy = new ByteVector(vector); //[2, 3, 4, 5, 6]
assert.equal(copy.length, 5);
assert.equal(copy.front(), 2);
assert.equal(copy.back(), 6);
});
});