Skip to content

Commit 8fff169

Browse files
committed
add unit tests for some sigstring functions
1 parent de66477 commit 8fff169

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

spec/sigstring_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'fiddle'
2+
3+
RSpec.describe "sigstring functions" do
4+
describe "sigstrlen" do
5+
it "returns the length of a string" do
6+
str = "Hello, world!"
7+
len = SigString.sigstrlen(str)
8+
expect(len).to eq(str.length)
9+
end
10+
11+
it "returns the length of an empty string" do
12+
str = ""
13+
len = SigString.sigstrlen(str)
14+
expect(len).to eq(str.length)
15+
end
16+
end
17+
18+
describe "sigstrcpy" do
19+
it "copies a string from one location to another" do
20+
str = "Hello, world!"
21+
buffer = Fiddle::Pointer.malloc(str.length + 1, Fiddle::RUBY_FREE)
22+
SigString.sigstrcpy(buffer, str)
23+
expect(buffer.to_s).to eq(str)
24+
end
25+
end
26+
27+
describe "sigstrncpy" do
28+
it "copies a portion of a string from one location to another" do
29+
str = "Hello, world!"
30+
n_chars = 5
31+
buffer = Fiddle::Pointer.malloc(n_chars + 1, Fiddle::RUBY_FREE)
32+
SigString.sigstrncpy(buffer, str, n_chars)
33+
expect(buffer.to_s).to eq("Hello")
34+
end
35+
36+
it "copies a string to the end of a buffer" do
37+
str = "Hello, world!"
38+
n_chars = 24
39+
buffer = Fiddle::Pointer.malloc(n_chars, Fiddle::RUBY_FREE)
40+
SigString.sigstrncpy(buffer, str, n_chars)
41+
expect(buffer.to_s).to eq(str)
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)