forked from elixirscript/elixirscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbit_string.spec.js
More file actions
63 lines (49 loc) · 1.94 KB
/
bit_string.spec.js
File metadata and controls
63 lines (49 loc) · 1.94 KB
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
import Core from "../lib/core";
const Patterns = Core.Patterns;
const BitString = Core.BitString;
import Enum from "../lib/enum";
import chai from 'chai';
var expect = chai.expect;
function is_match(pattern, expr, guard = () => true){
return Patterns.match_no_throw(pattern, expr, guard) != null;
}
describe('BitString', function(){
describe('creation', function(){
it('create properly', function(){
let bs = new BitString(BitString.integer(1));
expect(is_match(bs.value, [1])).to.equal(true);
bs = new BitString(BitString.binary("foo"));
expect(is_match(bs.value, [102, 111, 111])).to.equal(true);
bs = new BitString(BitString.integer(0), BitString.binary("foo"));
expect(is_match(bs.value, [0, 102, 111, 111])).to.equal(true);
bs = new BitString(BitString.float(3.14));
expect(is_match(bs.value, [64, 9, 30, 184, 81, 235, 133, 31])).to.equal(true);
bs = new BitString(BitString.signed(BitString.integer(-100)));
expect(is_match(bs.value, [156])).to.equal(true);
});
});
describe('UTF conversions', function(){
it('toUTF8Array', function(){
let bs = BitString.toUTF8Array("fo≈");
expect(is_match(bs, [102, 111, 226, 137, 136])).to.equal(true);
});
it('toUTF16Array', function(){
let bs = BitString.toUTF16Array("fo≈");
expect(is_match(bs, [0, 102, 0, 111, 34, 72])).to.equal(true);
});
it('toUTF32Array', function(){
let bs = BitString.toUTF32Array("fo≈");
expect(is_match(bs, [0, 0, 0, 102, 0, 0, 0, 111, 0, 0, 34, 72])).to.equal(true);
});
});
describe('Float conversions', function(){
it('float32ToBytes', function(){
let bs = BitString.float32ToBytes(3.14);
expect(is_match(bs, [64, 72, 245, 195])).to.equal(true);
});
it('float64ToBytes', function(){
let bs = BitString.float64ToBytes(3.14);
expect(is_match(bs, [64, 9, 30, 184, 81, 235, 133, 31])).to.equal(true);
});
});
});