lib/test_ids/allocator.rb in test_ids-0.7.1 vs lib/test_ids/allocator.rb in test_ids-0.8.0
- old
+ new
@@ -14,10 +14,11 @@
end
# Main method to inject generated bin and test numbers, the given
# options instance is modified accordingly
def allocate(instance, options)
+ orig_options = options.dup
clean(options)
@callbacks = []
name = extract_test_name(instance, options)
name = "#{name}_#{options[:index]}" if options[:index]
@@ -91,14 +92,23 @@
end
# Otherwise generate the missing ones
bin['number'] ||= allocate_bin(size: bin_size)
bin['size'] ||= bin_size
- softbin['number'] ||= allocate_softbin(bin: bin['number'], size: softbin_size)
- softbin['size'] ||= softbin_size
- number['number'] ||= allocate_number(bin: bin['number'], softbin: softbin['number'], size: number_size)
- number['size'] ||= number_size
+ # If the softbin is based on the test number, then need to calculate the
+ # test number first
+ if config.softbins.algorithm && config.softbins.algorithm.to_s =~ /n/
+ number['number'] ||= allocate_number(bin: bin['number'], size: number_size)
+ number['size'] ||= number_size
+ softbin['number'] ||= allocate_softbin(bin: bin['number'], number: number['number'], size: softbin_size)
+ softbin['size'] ||= softbin_size
+ else
+ softbin['number'] ||= allocate_softbin(bin: bin['number'], size: softbin_size)
+ softbin['size'] ||= softbin_size
+ number['number'] ||= allocate_number(bin: bin['number'], softbin: softbin['number'], size: number_size)
+ number['size'] ||= number_size
+ end
# Record that there has been a reference to the final numbers
time = Time.now.to_f
bin_size.times do |i|
store['references']['bin'][(bin['number'] + i).to_s] = time if bin['number'] && options[:bin] != :none
@@ -121,10 +131,22 @@
end
unless options.delete(:number) == :none
options[:number] = number['number']
options[:number_size] = number['size']
end
+
+ ## If reallocation is on, then check if the generated numbers are compliant, if not
+ ## clear them and go back around again to generate a new set
+ # if TestIds.reallocate_non_compliant
+ # if !config.bins.function?
+ # if !config.bins.compliant?(options[:bin])
+ # store["assigned"]["bin"].delete(bin_id)
+ # return allocate(instance, orig_options)
+ # end
+ # end
+ # end
+
options
end
def store
@store ||= begin
@@ -160,10 +182,32 @@
}
end
end
end
+ # Clear the :bins, :softbins and/or :numbers by setting the options for each item to true
+ def clear(options)
+ if options[:softbin] || options[:softbins]
+ store['assigned']['softbin'] = {}
+ store['manually_assigned']['softbin'] = {}
+ store['pointers']['softbin'] = nil
+ store['references']['softbin'] = {}
+ end
+ if options[:bin] || options[:bins]
+ store['assigned']['bin'] = {}
+ store['manually_assigned']['bin'] = {}
+ store['pointers']['bin'] = nil
+ store['references']['bin'] = {}
+ end
+ if options[:number] || options[:numbers]
+ store['assigned']['number'] = {}
+ store['manually_assigned']['number'] = {}
+ store['pointers']['number'] = nil
+ store['references']['number'] = {}
+ end
+ end
+
# Saves the current allocator state to the repository
def save
if file
# Ensure the current store has been loaded before we try to re-write it, this
# is necessary if the program generator has crashed before creating a test
@@ -218,21 +262,30 @@
end
end
def allocate_softbin(options)
bin = options[:bin]
+ num = options[:number]
return nil if config.softbins.empty?
if config.softbins.algorithm
algo = config.softbins.algorithm.to_s.downcase
- if algo.to_s =~ /^[b\dx]+$/
+ if algo.to_s =~ /^[b\dxn]+$/
number = algo.to_s
bin = bin.to_s
if number =~ /(b+)/
max_bin_size = Regexp.last_match(1).size
if bin.size > max_bin_size
- fail "Bin number (#{bin}) overflows the test number algorithm (#{algo})"
+ fail "Bin number (#{bin}) overflows the softbin number algorithm (#{algo})"
end
number = number.sub(/b+/, bin.rjust(max_bin_size, '0'))
+ end
+ if number =~ /(n+)/
+ num = num.to_s
+ max_num_size = Regexp.last_match(1).size
+ if num.size > max_num_size
+ fail "Test number (#{num}) overflows the softbin number algorithm (#{algo})"
+ end
+ number = number.sub(/n+/, num.rjust(max_num_size, '0'))
end
if number =~ /(x+)/
max_counter_size = Regexp.last_match(1).size
refs = store['references']['softbin']
i = 0