/
setbit_spec.rb
55 lines (45 loc) · 1.42 KB
/
setbit_spec.rb
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
require 'spec_helper'
describe '#setbit(key, offset)' do
before do
Encoding.default_external = 'UTF-8'
@key = 'mock-redis-test:setbit'
@redises.set(@key, 'h') # ASCII 0x68
end
it "returns the original stored bit's value" do
@redises.setbit(@key, 0, 1).should == 0
@redises.setbit(@key, 1, 1).should == 1
end
it 'sets the bit within the string' do
@redises.setbit(@key, 7, 1)
@redises.get(@key).should == 'i' # ASCII 0x69
end
it 'unsets the bit within the string' do
@redises.setbit(@key, 1, 0)
@redises.get(@key).should == '(' # ASCII 0x28
end
it 'does the right thing with multibyte characters' do
@redises.set(@key, '€99.94') # the euro sign is 3 bytes wide in UTF-8
@redises.setbit(@key, 63, 1).should == 0
@redises.get(@key).should == '€99.95'
end
it 'expands the string if necessary' do
@redises.setbit(@key, 9, 1)
@redises.get(@key).should == 'h@'
end
it 'sets added bits to 0' do
@redises.setbit(@key, 17, 1)
@redises.get(@key).should == "h\000@"
end
it 'treats missing keys as empty strings' do
@redises.del(@key)
@redises.setbit(@key, 1, 1)
@redises.get(@key).should == '@'
end
it 'sets and retrieves bits' do
@redises.setbit(@key, 22, 1)
@redises.getbit(@key, 22).should == 1
@redises.setbit(@key, 23, 0)
@redises.getbit(@key, 23).should == 0
end
it_should_behave_like 'a string-only command'
end