Skip to content

Commit

Permalink
Fix syntax warnings and allocation specs
Browse files Browse the repository at this point in the history
  • Loading branch information
kputnam committed Sep 28, 2019
1 parent 925c777 commit 9e91796
Show file tree
Hide file tree
Showing 11 changed files with 458 additions and 243 deletions.
9 changes: 6 additions & 3 deletions lib/stupidedi/reader/pointer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def +(other)
end

if (@storage.equal?(o_storage) and @offset + @length == o_offset) \
or subseq_eq?(@storage, @offset + @length, o_storage, o_offset, o_length)
or subseq_eq?(@storage, @offset + @length, o_storage, o_offset, o_length)
self.class.new(@storage.freeze, @offset, @length + o_length)
else
if other.is_a?(self.class)
Expand Down Expand Up @@ -338,6 +338,9 @@ def inspect
# This operation typically allocates memory and copies part of @storage,
# so this is avoided as much as possible.
#
# Unless the optional parameter `always_allocate` is `true`, then the
# return value may be `#frozen?` in some cases.
#
# @return [S]
def reify(always_allocate = false)
if @storage.frozen? \
Expand Down Expand Up @@ -378,13 +381,13 @@ class << Pointer
#########################################################################

# Constructs a new Pointer depending on what type of object is given.
#
#
# NOTE: Pointer operations can potentially destrucively modify the given
# object, but if it is `#frozen?`, a copy will be made before the update.
# If you are accessing or modifying the object outside of the Pointer API,
# unexpected results might occur. To avoid this, either provide a copy
# with `#dup` or freeze the object first with `#freeze`.
#
#
# @return [Pointer]
def build(object)
case object
Expand Down
5 changes: 3 additions & 2 deletions lib/stupidedi/reader/substring.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ def count(other)
# substring pointer.
#
# @return [self]
Z = "abc"
def <<(other)
case other
when self.class
Expand All @@ -185,7 +186,7 @@ def <<(other)
@length += other.length
elsif not @storage.frozen?
# Surely no one will notice if we destructively update @storage
@storage[@offset + @length, @storage.length - @offset - @length] = other
@storage[@offset + @length, @storage.length - @offset - @length] = other.reify
@length += other.length
else
# Other pointers are sharing our storage. We need to make our own
Expand Down Expand Up @@ -398,7 +399,7 @@ def _match(pattern, offset, anchorless=false)

offset = @length if offset > length
offset = @offset + offset
m = pattern.match(@storage, offset)
m = @storage.match(pattern, offset)

if m and m.begin(0) <= @offset + @length
if m.end(0) <= @offset + @length
Expand Down
2 changes: 1 addition & 1 deletion lib/stupidedi/reader/tokenizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ def _update_state(segment_tok, config)
gscode = version.try(:slice, 0, 6)

# GS01: Functional Identifier Code
fgcode = segment_tok.element_toks.at(0).try(:value)
# fgcode = segment_tok.element_toks.at(0).try(:value)

if config.functional_group.defined_at?(gscode)
envelope_def = config.functional_group.at(gscode)
Expand Down
1 change: 1 addition & 0 deletions lib/stupidedi/ruby/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def match?(pattern, pos=nil)
if pos.nil?
!!(self =~ pattern)
else
# NOTE: Regexp#match allocates a MatchData, String#match does not
!!match(pattern, pos)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/stupidedi/reader/native_ext_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
expect(extend_language).to be_graphic(encoding: e)
end

if n = e[/iso-8859-(\d+)/, 1].try{|n| Integer(n) }
if n = e[/iso-8859-(\d+)/, 1].try{|m| Integer(m) }
it "identifies all graphic characters" do
bytes = iso_8859_table.reject{|_, no| no.include?(n) }.keys
string = bytes.sort.map(&:chr).join.force_encoding(e)
Expand Down
129 changes: 71 additions & 58 deletions spec/lib/stupidedi/reader/pointer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# frozen_string_literal: true
# frozen_string_literal: false

describe Stupidedi::Reader::Pointer do
using Stupidedi::Refinements

def pointer(value)
Stupidedi::Reader::Pointer.build(value)
def pointer(string, frozen: nil)
frozen = [true, false].sample if frozen.nil?
string.freeze if frozen and not string.frozen?
string = string.dup if not frozen and string.frozen?
Stupidedi::Reader::Pointer.build(string)
end


def pointer_(*args)
Stupidedi::Reader::Pointer.new(args)
end
Expand All @@ -23,12 +28,12 @@ def suffix(pointer)

describe ".build" do
context "when value is a String" do
specify { expect(pointer("abc")).to be_a(Stupidedi::Reader::Pointer) }
specify { expect(pointer("111")).to be_a(Stupidedi::Reader::Pointer) }

allocation do
ignore = nil
storage = [1,1,1]
expect{ ignore = pointer(storage) }.to allocate(Stupidedi::Reader::Pointer => 1)
storage = "111"
expect{ ignore = pointer(storage, frozen: true) }.to allocate(Stupidedi::Reader::Substring => 1)
end
end

Expand All @@ -37,7 +42,7 @@ def suffix(pointer)

allocation do
storage = [1,2]
expect{ pointer(storage) }.to allocate(Stupidedi::Reader::Pointer => 1)
expect{ pointer(storage, frozen: true) }.to allocate(Stupidedi::Reader::Pointer => 1)
end
end

Expand Down Expand Up @@ -76,24 +81,39 @@ def suffix(pointer)

describe "#reify" do
context "when storage is frozen" do
let(:abcdef) { pointer("abcdef".freeze) }
let(:p) { pointer("abcdef", frozen: true) }

context "and storage spans entire storage" do
allocation { p = abcdef; expect{ p.send(:reify) }.to allocate(String: 0) }
allocation { p = abcdef; expect{ p.send(:reify, false) }.to allocate(String: 0) }
allocation { p; expect{ p.send(:reify) }.to allocate(String: 0) }
allocation { p; expect{ p.send(:reify, false) }.to allocate(String: 0) }

context "but always_allocate is true" do
allocation { p = abcdef; expect{ p.send(:reify, true) }.to allocate(String: 1) }
allocation { p; expect{ p.send(:reify, true) }.to allocate(String: 1) }
end
end

context "and storage does not span entire storage" do
allocate { p = abcdef.drop(1); expect{ p.send(:reify) }.to allocate(String: 1) }
allocate { p = abcdef.take(1); expect{ p.send(:reify) }.to allocate(String: 1) }
allocate { p = p.drop(1); expect{ p.send(:reify) }.to allocate(String: 1) }
allocate { p = p.take(1); expect{ p.send(:reify) }.to allocate(String: 1) }
end
end

todo "when storage is not frozen" do
context "when storage is not frozen" do
let(:p) { pointer("abcdef", frozen: false) }

context "and storage spans entire storage" do
allocation { p; expect{ p.send(:reify) }.to allocate(String: 1) }
allocation { p; expect{ p.send(:reify, false) }.to allocate(String: 1) }

context "but always_allocate is true" do
allocation { p; expect{ p.send(:reify, true) }.to allocate(String: 1) }
end
end

context "and storage does not span entire storage" do
allocate { p = p.drop(1); expect{ p.send(:reify) }.to allocate(String: 1) }
allocate { p = p.take(1); expect{ p.send(:reify) }.to allocate(String: 1) }
end
end
end

Expand Down Expand Up @@ -159,11 +179,10 @@ def suffix(pointer)
end

describe "+" do
let(:a) { pointer("abcdefghi".dup) }

context "when argument is a non-pointer value" do
context "when pointer suffix starts with argument" do
specify do
a = pointer("abcdefghi")
b = a.drop(3).take(3)
c = "gh"

Expand All @@ -178,15 +197,17 @@ def suffix(pointer)
end

allocation do
a = pointer("abcdefghi")
b = a.drop(3).take(3)
c = "gh"
expect(suffix(b)).to start_with(c)
expect{ b + c }.to allocate(String: 0, a.class => 1)
expect{ b + c }.to allocate(b.class => 1)
end
end

context "when argument is pointer suffix plus more" do
specify do
a = pointer("abcdefghi")
b = a.drop(3).take(3)
c = "ghijkl"

Expand All @@ -203,6 +224,7 @@ def suffix(pointer)
end

allocation do
a = pointer("abcdefghi")
b = a.drop(3).take(3)
c = "ghijkl"
expect(c).to start_with(suffix(b))
Expand All @@ -213,6 +235,7 @@ def suffix(pointer)

context "when argument is not pointer suffix" do
specify do
a = pointer("abcdefghi")
b = a.take(6)
c = "xxx"

Expand All @@ -228,6 +251,7 @@ def suffix(pointer)
end

allocation do
a = pointer("abcdefghi")
b = a.take(6)
c = "xxx"
expect(a.storage).to be_frozen
Expand All @@ -239,6 +263,7 @@ def suffix(pointer)
context "when argument is a string pointer" do
context "when pointer suffix starts with argument" do
specify do
a = pointer("abcdefghi")
b = a.drop(3).take(3)
c = pointer("gh")

Expand All @@ -254,15 +279,17 @@ def suffix(pointer)
end

allocation do
a = pointer("abcdefghi")
b = a.drop(3).take(3)
c = pointer("gh")
expect(suffix(b)).to start_with(c)
expect{ b + c }.to allocate(a.class => 1)
expect{ b + c }.to allocate(b.class => 1)
end
end

context "when argument is pointer suffix plus more" do
specify do
a = pointer("abcdefghi")
b = a.drop(3).take(3)
c = pointer("ghijkl")

Expand All @@ -279,6 +306,7 @@ def suffix(pointer)
end

allocation do
a = pointer("abcdefghi")
b = a.drop(3).take(3)
c = pointer("ghijkl")
expect(c).to start_with(suffix(b))
Expand All @@ -289,6 +317,7 @@ def suffix(pointer)

context "when argument is not pointer suffix" do
specify do
a = pointer("abcdefghi")
b = a.take(6)
c = pointer("xxx")

Expand All @@ -304,51 +333,35 @@ def suffix(pointer)
end

allocation do
a = pointer("abcdefghi")
b = a.take(6)
c = pointer("xxx")
c = pointer("xxx", frozen: true)
expect(suffix(b)).to_not start_with(c)
expect{ b + c }.to allocate(String: 1)
expect{ b + c }.to allocate(String: 1 + (c.storage.frozen? && 0 || 1))
end

allocation do
a = pointer("abcdefghi")
b = a.take(6)
c = pointer("xxx", frozen: false)
expect(suffix(b)).to_not start_with(c)
expect{ b + c }.to allocate(String: 1 + (c.storage.frozen? && 0 || 1))
end
end
end
end

describe "#head" do
end

describe "#last" do
end

describe "#defined_at?" do
end

describe "#at" do
end

describe "#tail" do
end

describe "#[]" do
end

describe "#drop" do
end

describe "#drop!" do
end

describe "take" do
end

context "#take!" do
end

describe "#drop_take" do
end

describe "#split_at" do
end

describe ".build" do
end
todo "#head"
todo "#last"
todo "#defined_at?"
todo "#at"
todo "#tail"
todo "#[]"
todo "#drop"
todo "#drop!"
todo "take"
todo "#take!"
todo "#drop_take"
todo "#split_at"
todo ".build"
end
Loading

0 comments on commit 9e91796

Please sign in to comment.