diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/04-term_spec.lua | 20 | ||||
-rw-r--r-- | spec/05-bitflags_spec.lua | 108 |
2 files changed, 128 insertions, 0 deletions
diff --git a/spec/04-term_spec.lua b/spec/04-term_spec.lua index a2034aa..9ca37e9 100644 --- a/spec/04-term_spec.lua +++ b/spec/04-term_spec.lua | |||
@@ -91,4 +91,24 @@ describe("Terminal:", function() | |||
91 | 91 | ||
92 | end) | 92 | end) |
93 | 93 | ||
94 | |||
95 | |||
96 | describe("getconsoleflags()", function() | ||
97 | |||
98 | pending("returns the consoleflags, if called without flags", function() | ||
99 | print"1" | ||
100 | package.loaded["system"] = nil | ||
101 | package.loaded["system.core"] = nil | ||
102 | print"2" | ||
103 | local system = require "system" | ||
104 | print"3" | ||
105 | for k,v in pairs(system) do print(k,v) end | ||
106 | for k,v in pairs(debug.getinfo(system.isatty)) do print(k,v) end | ||
107 | |||
108 | local flags, err = system.getconsoleflags(io.stdin) | ||
109 | assert.is_nil(err) | ||
110 | assert.is_integer(flags) | ||
111 | end) | ||
112 | |||
113 | end) | ||
94 | end) | 114 | end) |
diff --git a/spec/05-bitflags_spec.lua b/spec/05-bitflags_spec.lua new file mode 100644 index 0000000..01bf958 --- /dev/null +++ b/spec/05-bitflags_spec.lua | |||
@@ -0,0 +1,108 @@ | |||
1 | describe("BitFlags library", function() | ||
2 | |||
3 | local sys = require("system") | ||
4 | |||
5 | it("creates new flag objects", function() | ||
6 | local bf = sys.bitflag(255) | ||
7 | assert.is_not_nil(bf) | ||
8 | assert.are.equal(255, bf:value()) | ||
9 | assert.is.userdata(bf) | ||
10 | end) | ||
11 | |||
12 | it("converts to a hex string", function() | ||
13 | local bf = sys.bitflag(255) | ||
14 | assert.are.equal("bitflags: 255", tostring(bf)) | ||
15 | end) | ||
16 | |||
17 | it("handles OR/ADD operations", function() | ||
18 | -- one at a time | ||
19 | local bf1 = sys.bitflag(1) -- b0001 | ||
20 | local bf2 = sys.bitflag(2) -- b0010 | ||
21 | local bf3 = bf1 + bf2 -- b0011 | ||
22 | assert.are.equal(3, bf3:value()) | ||
23 | -- multiple at once | ||
24 | local bf4 = sys.bitflag(4+8) -- b1100 | ||
25 | local bf5 = bf3 + bf4 -- b1111 | ||
26 | assert.are.equal(15, bf5:value()) | ||
27 | -- multiple that were already set | ||
28 | local bf6 = sys.bitflag(15) -- b1111 | ||
29 | local bf7 = sys.bitflag(8+2) -- b1010 | ||
30 | local bf8 = bf6 + bf7 -- b1111 | ||
31 | assert.are.equal(15, bf8:value()) | ||
32 | end) | ||
33 | |||
34 | it("handles AND-NOT/SUBSTRACT operations", function() | ||
35 | -- one at a time | ||
36 | local bf1 = sys.bitflag(3) -- b0011 | ||
37 | local bf2 = sys.bitflag(1) -- b0001 | ||
38 | local bf3 = bf1 - bf2 -- b0010 | ||
39 | assert.are.equal(2, bf3:value()) | ||
40 | -- multiple at once | ||
41 | local bf4 = sys.bitflag(15) -- b1111 | ||
42 | local bf5 = sys.bitflag(8+2) -- b1010 | ||
43 | local bf6 = bf4 - bf5 -- b0101 | ||
44 | assert.are.equal(5, bf6:value()) | ||
45 | -- multiple that were not set | ||
46 | local bf7 = sys.bitflag(3) -- b0011 | ||
47 | local bf8 = sys.bitflag(15) -- b1111 | ||
48 | local bf9 = bf7 - bf8 -- b0000 | ||
49 | assert.are.equal(0, bf9:value()) | ||
50 | end) | ||
51 | |||
52 | it("checks for equality", function() | ||
53 | local bf1 = sys.bitflag(4) | ||
54 | local bf2 = sys.bitflag(4) | ||
55 | local bf3 = sys.bitflag(5) | ||
56 | assert.is.True(bf1 == bf2) | ||
57 | assert.is.False(bf1 == bf3) | ||
58 | end) | ||
59 | |||
60 | it("indexes bits correctly", function() | ||
61 | local bf = sys.bitflag(4) -- b100 | ||
62 | assert.is_true(bf[2]) | ||
63 | assert.is_false(bf[1]) | ||
64 | end) | ||
65 | |||
66 | it("errors on reading invalid bit indexes", function() | ||
67 | local bf = sys.bitflag(4) | ||
68 | assert.has_error(function() return bf[-10] end, "index out of range") | ||
69 | assert.has_error(function() return bf[10000] end, "index out of range") | ||
70 | assert.has_no_error(function() return bf.not_a_number end) | ||
71 | end) | ||
72 | |||
73 | it("sets and clears bits correctly", function() | ||
74 | local bf = sys.bitflag(0) | ||
75 | bf[1] = true | ||
76 | assert.is_true(bf[1]) | ||
77 | bf[1] = false | ||
78 | assert.is_false(bf[1]) | ||
79 | end) | ||
80 | |||
81 | it("errors on setting invalid bit indexes", function() | ||
82 | local bf = sys.bitflag(0) | ||
83 | assert.has_error(function() bf[-10] = true end, "index out of range") | ||
84 | assert.has_error(function() bf[10000] = true end, "index out of range") | ||
85 | assert.has_error(function() bf.not_a_number = true end, "index must be a number") | ||
86 | end) | ||
87 | |||
88 | it("handles <= and >= operations", function() | ||
89 | local bf1 = sys.bitflag(3) -- b0011 | ||
90 | local bf2 = sys.bitflag(15) -- b1111 | ||
91 | assert.is_true(bf2 >= bf1) -- all bits in bf1 are set in bf2 | ||
92 | assert.is_true(bf2 > bf1) -- all bits in bf1 are set in bf2 and some more | ||
93 | assert.is_false(bf2 <= bf1) -- not all bits in bf2 are set in bf1 | ||
94 | assert.is_false(bf2 < bf1) -- not all bits in bf2 are set in bf1 | ||
95 | end) | ||
96 | |||
97 | it("checks for a subset using 'has'", function() | ||
98 | local bf1 = sys.bitflag(3) -- b0011 | ||
99 | local bf2 = sys.bitflag(3) -- b0011 | ||
100 | local bf3 = sys.bitflag(15) -- b1111 | ||
101 | local bf0 = sys.bitflag(0) -- b0000 | ||
102 | assert.is_true(bf1:has(bf2)) -- equal | ||
103 | assert.is_true(bf3:has(bf1)) -- is a subset, and has more flags | ||
104 | assert.is_false(bf1:has(bf3)) -- not a subset, bf3 has more flags | ||
105 | assert.is_false(bf1:has(bf0)) -- bf0 is unset, always returns false | ||
106 | end) | ||
107 | |||
108 | end) | ||