diff --git a/docs/license.txt b/LICENSE.txt similarity index 100% rename from docs/license.txt rename to LICENSE.txt diff --git a/auto/colour_prompt.rb b/auto/colour_prompt.rb index 0f1dc4e025d919c41a1a61dfe950621253600d37..bf09d028d5a1bb878981136280d4d4959248d81b 100644 --- a/auto/colour_prompt.rb +++ b/auto/colour_prompt.rb @@ -24,7 +24,7 @@ class ColourCommandLine return unless RUBY_PLATFORM =~ /(win|w)32$/ get_std_handle = Win32API.new('kernel32', 'GetStdHandle', ['L'], 'L') @set_console_txt_attrb = - Win32API.new('kernel32', 'SetConsoleTextAttribute', %w(L N), 'I') + Win32API.new('kernel32', 'SetConsoleTextAttribute', %w[L N], 'I') @hout = get_std_handle.call(-11) end @@ -107,7 +107,7 @@ class ColourCommandLine $stdout.print("#{change_to(colour)}#{str}\033[0m") if mode == :print end end -end # ColourCommandLine +end def colour_puts(role, str) ColourCommandLine.new.out_c(:puts, role, str) diff --git a/auto/colour_reporter.rb b/auto/colour_reporter.rb index bb1fbfce34715544b15d63ec11b827eccf298a4a..1c3bc2162703b5b6041c6f1fcb2d0030da7f6d7d 100644 --- a/auto/colour_reporter.rb +++ b/auto/colour_reporter.rb @@ -4,7 +4,7 @@ # [Released under MIT License. Please refer to license.txt for details] # ========================================== -require "#{File.expand_path(File.dirname(__FILE__))}/colour_prompt" +require_relative 'colour_prompt' $colour_output = true diff --git a/auto/generate_module.rb b/auto/generate_module.rb index 13b4cc74d7ff084158aa800c5a8cfa218ed5c306..fb8c7ac47571b70ef8a3782e2184d1ce0299d69c 100644 --- a/auto/generate_module.rb +++ b/auto/generate_module.rb @@ -45,8 +45,6 @@ TEMPLATE_INC ||= '#ifndef _%3$s_H class UnityModuleGenerator ############################ def initialize(options = nil) - here = File.expand_path(File.dirname(__FILE__)) + '/' - @options = UnityModuleGenerator.default_options case options when NilClass then @options @@ -56,9 +54,9 @@ class UnityModuleGenerator end # Create default file paths if none were provided - @options[:path_src] = here + '../src/' if @options[:path_src].nil? - @options[:path_inc] = @options[:path_src] if @options[:path_inc].nil? - @options[:path_tst] = here + '../test/' if @options[:path_tst].nil? + @options[:path_src] = "#{__dir__}/../src/" if @options[:path_src].nil? + @options[:path_inc] = @options[:path_src] if @options[:path_inc].nil? + @options[:path_tst] = "#{__dir__}/../test/" if @options[:path_tst].nil? @options[:path_src] += '/' unless @options[:path_src][-1] == 47 @options[:path_inc] += '/' unless @options[:path_inc][-1] == 47 @options[:path_tst] += '/' unless @options[:path_tst][-1] == 47 diff --git a/auto/generate_test_runner.rb b/auto/generate_test_runner.rb index 2f149665e527e080f243cbe99e22d81de75273ca..2acda0f3c3de7ea2212f81f48fd67a5987042446 100644 --- a/auto/generate_test_runner.rb +++ b/auto/generate_test_runner.rb @@ -4,8 +4,6 @@ # [Released under MIT License. Please refer to license.txt for details] # ========================================== -File.expand_path(File.join(File.dirname(__FILE__), 'colour_prompt')) - class UnityTestRunnerGenerator def initialize(options = nil) @options = UnityTestRunnerGenerator.default_options @@ -15,7 +13,7 @@ class UnityTestRunnerGenerator when Hash then @options.merge!(options) else raise 'If you specify arguments, it should be a filename or a hash of options' end - require "#{File.expand_path(File.dirname(__FILE__))}/type_sanitizer" + require_relative 'type_sanitizer' end def self.default_options @@ -92,12 +90,25 @@ class UnityTestRunnerGenerator def find_tests(source) tests_and_line_numbers = [] + # contains characters which will be substituted from within strings, doing + # this prevents these characters from interferring with scrubbers + # @ is not a valid C character, so there should be no clashes with files genuinely containing these markers + substring_subs = { '{' => '@co@', '}' => '@cc@', ';' => '@ss@', '/' => '@fs@' } + substring_re = Regexp.union(substring_subs.keys) + substring_unsubs = substring_subs.invert # the inverse map will be used to fix the strings afterwords + substring_unsubs['@quote@'] = '\\"' + substring_unsubs['@apos@'] = '\\\'' + substring_unre = Regexp.union(substring_unsubs.keys) source_scrubbed = source.clone - source_scrubbed = source_scrubbed.gsub(/"[^"\n]*"/, '') # remove things in strings + source_scrubbed = source_scrubbed.gsub(/\\"/, '@quote@') # hide escaped quotes to allow capture of the full string/char + source_scrubbed = source_scrubbed.gsub(/\\'/, '@apos@') # hide escaped apostrophes to allow capture of the full string/char + source_scrubbed = source_scrubbed.gsub(/("[^"\n]*")|('[^'\n]*')/) { |s| s.gsub(substring_re, substring_subs) } # temporarily hide problematic + # characters within strings source_scrubbed = source_scrubbed.gsub(/\/\/.*$/, '') # remove line comments source_scrubbed = source_scrubbed.gsub(/\/\*.*?\*\//m, '') # remove block comments lines = source_scrubbed.split(/(^\s*\#.*$) # Treat preprocessor directives as a logical line | (;|\{|\}) /x) # Match ;, {, and } as end of lines + .map { |line| line.gsub(substring_unre, substring_unsubs) } # unhide the problematic characters previously removed lines.each_with_index do |line, _index| # find tests @@ -165,10 +176,10 @@ class UnityTestRunnerGenerator output.puts('#include "cmock.h"') unless mocks.empty? output.puts('#ifndef UNITY_EXCLUDE_SETJMP_H') output.puts('#include ') - output.puts("#endif") + output.puts('#endif') output.puts('#include ') if @options[:defines] && !@options[:defines].empty? - @options[:defines].each { |d| output.puts("#define #{d}") } + @options[:defines].each { |d| output.puts("#ifndef #{d}\n#define #{d}\n#endif /* #{d} */") } end if @options[:header_file] && !@options[:header_file].empty? output.puts("#include \"#{File.basename(@options[:header_file])}\"") @@ -379,7 +390,7 @@ class UnityTestRunnerGenerator end output.puts output.puts(' CMock_Guts_MemFreeFinal();') unless used_mocks.empty? - output.puts(" return suite_teardown(UnityEnd());") + output.puts(' return suite_teardown(UnityEnd());') output.puts('}') end diff --git a/auto/parse_output.rb b/auto/parse_output.rb index f04508fb0f87eaf88ee5e4456e06a0cb7335782c..fa07b7dbecea6b4e67464a684cac5ec80b79c406 100644 --- a/auto/parse_output.rb +++ b/auto/parse_output.rb @@ -210,7 +210,7 @@ class ParseOutput # Adjusts the os specific members according to the current path style # (Windows or Unix based) - def set_os_specifics(line) + def detect_os_specifics(line) if line.include? '\\' # Windows X:\Y\Z @class_name_idx = 1 @@ -254,43 +254,42 @@ class ParseOutput # TEST() PASS # # Note: Where path is different on Unix vs Windows devices (Windows leads with a drive letter)! - set_os_specifics(line) + detect_os_specifics(line) line_array = line.split(':') # If we were able to split the line then we can look to see if any of our target words # were found. Case is important. - if (line_array.size >= 4) || (line.start_with? 'TEST(') || (line.start_with? 'IGNORE_TEST(') - - # check if the output is fixture output (with verbose flag "-v") - if (line.start_with? 'TEST(') || (line.start_with? 'IGNORE_TEST(') - line_array = prepare_fixture_line(line) - if line.include? ' PASS' - test_passed_unity_fixture(line_array) - @test_passed += 1 - elsif line.include? 'FAIL' - test_failed_unity_fixture(line_array) - @test_failed += 1 - elsif line.include? 'IGNORE' - test_ignored_unity_fixture(line_array) - @test_ignored += 1 - end - # normal output / fixture output (without verbose "-v") - elsif line.include? ':PASS' - test_passed(line_array) + next unless (line_array.size >= 4) || (line.start_with? 'TEST(') || (line.start_with? 'IGNORE_TEST(') + + # check if the output is fixture output (with verbose flag "-v") + if (line.start_with? 'TEST(') || (line.start_with? 'IGNORE_TEST(') + line_array = prepare_fixture_line(line) + if line.include? ' PASS' + test_passed_unity_fixture(line_array) @test_passed += 1 - elsif line.include? ':FAIL' - test_failed(line_array) + elsif line.include? 'FAIL' + test_failed_unity_fixture(line_array) @test_failed += 1 - elsif line.include? ':IGNORE:' - test_ignored(line_array) - @test_ignored += 1 - elsif line.include? ':IGNORE' - line_array.push('No reason given') - test_ignored(line_array) + elsif line.include? 'IGNORE' + test_ignored_unity_fixture(line_array) @test_ignored += 1 end - @total_tests = @test_passed + @test_failed + @test_ignored + # normal output / fixture output (without verbose "-v") + elsif line.include? ':PASS' + test_passed(line_array) + @test_passed += 1 + elsif line.include? ':FAIL' + test_failed(line_array) + @test_failed += 1 + elsif line.include? ':IGNORE:' + test_ignored(line_array) + @test_ignored += 1 + elsif line.include? ':IGNORE' + line_array.push('No reason given') + test_ignored(line_array) + @test_ignored += 1 end + @total_tests = @test_passed + @test_failed + @test_ignored end puts '' puts '=================== SUMMARY =====================' diff --git a/auto/stylize_as_junit.rb b/auto/stylize_as_junit.rb index b3d8f4097bacf6c9322805bff5939e941f634006..a53f85f75d71829eb2225f7bf180a38f1cefeb79 100755 --- a/auto/stylize_as_junit.rb +++ b/auto/stylize_as_junit.rb @@ -61,8 +61,8 @@ class ArgvParser opts.parse!(args) options - end # parse() -end # class OptparseExample + end +end class UnityToJUnit include FileUtils::Verbose @@ -155,10 +155,6 @@ class UnityToJUnit [Regexp.last_match(1).to_i, Regexp.last_match(2).to_i, Regexp.last_match(3).to_i] end - def here - File.expand_path(File.dirname(__FILE__)) - end - private def results_structure @@ -221,9 +217,9 @@ class UnityToJUnit def write_suites_footer(stream) stream.puts '' end -end # UnityToJUnit +end -if __FILE__ == $0 +if $0 == __FILE__ # parse out the command options options = ArgvParser.parse(ARGV) diff --git a/auto/test_file_filter.rb b/auto/test_file_filter.rb index aad28e38e77ff484ec2888e74f1a13a45cf412b0..5c3a79fc67958cda8883ae3e6917496f6db4f883 100644 --- a/auto/test_file_filter.rb +++ b/auto/test_file_filter.rb @@ -11,8 +11,8 @@ module RakefileHelpers def initialize(all_files = false) @all_files = all_files - return false unless @all_files - return false unless File.exist?('test_file_filter.yml') + return unless @all_files + return unless File.exist?('test_file_filter.yml') filters = YAML.load_file('test_file_filter.yml') @all_files = filters[:all_files] diff --git a/auto/unity_test_summary.rb b/auto/unity_test_summary.rb index b37dc5fa7cb3cfa084799320ccb1cfc5a29bb158..810fdbd1b387676bcdc27783e93a4353aec1c789 100644 --- a/auto/unity_test_summary.rb +++ b/auto/unity_test_summary.rb @@ -101,10 +101,6 @@ class UnityTestSummary raise "Couldn't parse test results: #{summary}" unless summary.find { |v| v =~ /(\d+) Tests (\d+) Failures (\d+) Ignored/ } [Regexp.last_match(1).to_i, Regexp.last_match(2).to_i, Regexp.last_match(3).to_i] end - - def here - File.expand_path(File.dirname(__FILE__)) - end end if $0 == __FILE__ diff --git a/examples/example_1/src/ProductionCode.c b/examples/example_1/src/ProductionCode.c index d039c3e896ae3aef849d987d59fb24ce0ba7be20..db128e5bd75f158beaf899bf8b24b228f52e20f9 100644 --- a/examples/example_1/src/ProductionCode.c +++ b/examples/example_1/src/ProductionCode.c @@ -4,14 +4,14 @@ int Counter = 0; int NumbersToFind[9] = { 0, 34, 55, 66, 32, 11, 1, 77, 888 }; /* some obnoxious array to search that is 1-based indexing instead of 0. */ -/* This function is supposed to search through NumbersToFind and find a particular number. - * If it finds it, the index is returned. Otherwise 0 is returned which sorta makes sense since - * NumbersToFind is indexed from 1. Unfortunately it's broken +/* This function is supposed to search through NumbersToFind and find a particular number. + * If it finds it, the index is returned. Otherwise 0 is returned which sorta makes sense since + * NumbersToFind is indexed from 1. Unfortunately it's broken * (and should therefore be caught by our tests) */ int FindFunction_WhichIsBroken(int NumberToFind) { int i = 0; - while (i <= 8) /* Notice I should have been in braces */ + while (i < 8) /* Notice I should have been in braces */ i++; if (NumbersToFind[i] == NumberToFind) /* Yikes! I'm getting run after the loop finishes instead of during it! */ return i; diff --git a/examples/example_3/rakefile.rb b/examples/example_3/rakefile.rb index bf9f42be126b3962d1ed60976286bb6ebb36c503..d0149299fb7fba229d47c5533e863d226bfb83c6 100644 --- a/examples/example_3/rakefile.rb +++ b/examples/example_3/rakefile.rb @@ -1,12 +1,9 @@ -HERE = File.expand_path(File.dirname(__FILE__)) + '/' -UNITY_ROOT = File.expand_path(File.dirname(__FILE__)) + '/../..' - require 'rake' require 'rake/clean' -require HERE + 'rakefile_helper' +require_relative 'rakefile_helper' TEMP_DIRS = [ - File.join(HERE, 'build') + File.join(__dir__, 'build') ].freeze TEMP_DIRS.each do |dir| @@ -32,8 +29,8 @@ task :summary do end desc 'Build and test Unity' -task all: %i(clean unit summary) -task default: %i(clobber all) +task all: %i[clean unit summary] +task default: %i[clobber all] task ci: [:default] task cruise: [:default] diff --git a/examples/example_3/rakefile_helper.rb b/examples/example_3/rakefile_helper.rb index a186cf0ff5260995993aff7cfa500e3566db14c8..f1f51d4f1970a1142a2343bf1c73ee33157f2fd8 100644 --- a/examples/example_3/rakefile_helper.rb +++ b/examples/example_3/rakefile_helper.rb @@ -1,8 +1,8 @@ require 'yaml' require 'fileutils' -require UNITY_ROOT + '/auto/unity_test_summary' -require UNITY_ROOT + '/auto/generate_test_runner' -require UNITY_ROOT + '/auto/colour_reporter' +require_relative '../../auto/unity_test_summary' +require_relative '../../auto/generate_test_runner' +require_relative '../../auto/colour_reporter' module RakefileHelpers C_EXTENSION = '.c'.freeze @@ -149,7 +149,7 @@ module RakefileHelpers def report_summary summary = UnityTestSummary.new - summary.root = HERE + summary.root = __dir__ results_glob = "#{$cfg['compiler']['build_path']}*.test*" results_glob.tr!('\\', '/') results = Dir[results_glob] diff --git a/extras/fixture/rakefile.rb b/extras/fixture/rakefile.rb index 7603e5745f57c75205dd7e494e11f403bdc1ca98..8b5319c58d9250c92dccfede6f22ae219175a291 100644 --- a/extras/fixture/rakefile.rb +++ b/extras/fixture/rakefile.rb @@ -4,15 +4,13 @@ # [Released under MIT License. Please refer to license.txt for details] # ========================================== -HERE = File.expand_path(File.dirname(__FILE__)) + '/' - require 'rake' require 'rake/clean' require 'rake/testtask' -require HERE + 'rakefile_helper' +require_relative 'rakefile_helper' TEMP_DIRS = [ - File.join(HERE, 'build') + File.join(__dir__, 'build') ].freeze TEMP_DIRS.each do |dir| @@ -33,10 +31,10 @@ task unit: [:prepare_for_tests] do end desc 'Build and test Unity Framework' -task all: %i(clean unit) -task default: %i(clobber all) -task ci: %i(no_color default) -task cruise: %i(no_color default) +task all: %i[clean unit] +task default: %i[clobber all] +task ci: %i[no_color default] +task cruise: %i[no_color default] desc 'Load configuration' task :config, :config_file do |_t, args| diff --git a/extras/fixture/rakefile_helper.rb b/extras/fixture/rakefile_helper.rb index c45b2393d1df2bf8be896b258d259b0265f86955..71e752b920c1d17659af5a79ed6d37af480fb2cc 100644 --- a/extras/fixture/rakefile_helper.rb +++ b/extras/fixture/rakefile_helper.rb @@ -6,9 +6,9 @@ require 'yaml' require 'fileutils' -require HERE + '../../auto/unity_test_summary' -require HERE + '../../auto/generate_test_runner' -require HERE + '../../auto/colour_reporter' +require_relative '../../auto/unity_test_summary' +require_relative '../../auto/generate_test_runner' +require_relative '../../auto/colour_reporter' module RakefileHelpers C_EXTENSION = '.c'.freeze @@ -16,7 +16,7 @@ module RakefileHelpers def load_configuration(config_file) return if $configured - $cfg_file = HERE + "../../test/targets/#{config_file}" unless config_file =~ /[\\|\/]/ + $cfg_file = "#{__dir__}/../../test/targets/#{config_file}" unless config_file =~ /[\\|\/]/ $cfg = YAML.load(File.read($cfg_file)) $colour_output = false unless $cfg['colour'] $configured = true if config_file != DEFAULT_CONFIG_FILE @@ -128,7 +128,7 @@ module RakefileHelpers def report_summary summary = UnityTestSummary.new - summary.root = HERE + summary.root = __dir__ results_glob = "#{$cfg['compiler']['build_path']}*.test*" results_glob.tr!('\\', '/') results = Dir[results_glob] @@ -145,9 +145,9 @@ module RakefileHelpers $cfg['compiler']['defines']['items'] = [] if $cfg['compiler']['defines']['items'].nil? # Get a list of all source files needed - src_files = Dir[HERE + 'src/*.c'] - src_files += Dir[HERE + 'test/*.c'] - src_files += Dir[HERE + 'test/main/*.c'] + src_files = Dir["#{__dir__}/src/*.c"] + src_files += Dir["#{__dir__}/test/*.c"] + src_files += Dir["#{__dir__}/test/main/*.c"] src_files << '../../src/unity.c' # Build object files diff --git a/src/unity.c b/src/unity.c index 0f71b91b0339f080ee6cc07d22865047ff784b4f..fa1847bab11f8f2b11018a35a3162b2d42af9e8b 100644 --- a/src/unity.c +++ b/src/unity.c @@ -319,7 +319,7 @@ void UnityPrintNumber(const UNITY_INT number_to_print) { /* A negative number, including MIN negative */ UNITY_OUTPUT_CHAR('-'); - number = (UNITY_UINT)(-number_to_print); + number = -number; } UnityPrintNumberUnsigned(number); } @@ -350,7 +350,9 @@ void UnityPrintNumberHex(const UNITY_UINT number, const char nibbles_to_print) int nibble; char nibbles = nibbles_to_print; if ((unsigned)nibbles > (2 * sizeof(number))) + { nibbles = 2 * sizeof(number); + } while (nibbles > 0) { @@ -396,13 +398,25 @@ void UnityPrintMask(const UNITY_UINT mask, const UNITY_UINT number) /*-----------------------------------------------*/ #ifndef UNITY_EXCLUDE_FLOAT_PRINT -/* This function prints a floating-point value in a format similar to - * printf("%.6g"). It can work with either single- or double-precision, - * but for simplicity, it prints only 6 significant digits in either case. - * Printing more than 6 digits accurately is hard (at least in the single- - * precision case) and isn't attempted here. */ +/* + * This function prints a floating-point value in a format similar to + * printf("%.7g") on a single-precision machine or printf("%.9g") on a + * double-precision machine. The 7th digit won't always be totally correct + * in single-precision operation (for that level of accuracy, a more + * complicated algorithm would be needed). + */ void UnityPrintFloat(const UNITY_DOUBLE input_number) { +#ifdef UNITY_INCLUDE_DOUBLE + static const int sig_digits = 9; + static const UNITY_INT32 min_scaled = 100000000; + static const UNITY_INT32 max_scaled = 1000000000; +#else + static const int sig_digits = 7; + static const UNITY_INT32 min_scaled = 1000000; + static const UNITY_INT32 max_scaled = 10000000; +#endif + UNITY_DOUBLE number = input_number; /* print minus sign (including for negative zero) */ @@ -413,32 +427,85 @@ void UnityPrintFloat(const UNITY_DOUBLE input_number) } /* handle zero, NaN, and +/- infinity */ - if (number == 0.0f) UnityPrint("0"); - else if (isnan(number)) UnityPrint("nan"); - else if (isinf(number)) UnityPrint("inf"); + if (number == 0.0f) + { + UnityPrint("0"); + } + else if (isnan(number)) + { + UnityPrint("nan"); + } + else if (isinf(number)) + { + UnityPrint("inf"); + } else { + UNITY_INT32 n_int = 0, n; int exponent = 0; int decimals, digits; - UNITY_INT32 n; - char buf[16]; + char buf[16] = {0}; + + /* + * Scale up or down by powers of 10. To minimize rounding error, + * start with a factor/divisor of 10^10, which is the largest + * power of 10 that can be represented exactly. Finally, compute + * (exactly) the remaining power of 10 and perform one more + * multiplication or division. + */ + if (number < 1.0f) + { + UNITY_DOUBLE factor = 1.0f; - /* scale up or down by powers of 10 */ - while (number < 100000.0f / 1e6f) { number *= 1e6f; exponent -= 6; } - while (number < 100000.0f) { number *= 10.0f; exponent--; } - while (number > 1000000.0f * 1e6f) { number /= 1e6f; exponent += 6; } - while (number > 1000000.0f) { number /= 10.0f; exponent++; } + while (number < (UNITY_DOUBLE)max_scaled / 1e10f) { number *= 1e10f; exponent -= 10; } + while (number * factor < (UNITY_DOUBLE)min_scaled) { factor *= 10.0f; exponent--; } + + number *= factor; + } + else if (number > (UNITY_DOUBLE)max_scaled) + { + UNITY_DOUBLE divisor = 1.0f; + + while (number > (UNITY_DOUBLE)min_scaled * 1e10f) { number /= 1e10f; exponent += 10; } + while (number / divisor > (UNITY_DOUBLE)max_scaled) { divisor *= 10.0f; exponent++; } + + number /= divisor; + } + else + { + /* + * In this range, we can split off the integer part before + * doing any multiplications. This reduces rounding error by + * freeing up significant bits in the fractional part. + */ + UNITY_DOUBLE factor = 1.0f; + n_int = (UNITY_INT32)number; + number -= (UNITY_DOUBLE)n_int; + + while (n_int < min_scaled) { n_int *= 10; factor *= 10.0f; exponent--; } + + number *= factor; + } /* round to nearest integer */ n = ((UNITY_INT32)(number + number) + 1) / 2; - if (n > 999999) + +#ifndef UNITY_ROUND_TIES_AWAY_FROM_ZERO + /* round to even if exactly between two integers */ + if ((n & 1) && ((UNITY_DOUBLE)n - number == 0.5f)) + n--; +#endif + + n += n_int; + + if (n >= max_scaled) { - n = 100000; + n = min_scaled; exponent++; } /* determine where to place decimal point */ - decimals = (exponent <= 0 && exponent >= -9) ? -exponent : 5; + decimals = (exponent <= 0 && exponent >= -(sig_digits + 3)) ? -exponent : (sig_digits - 1); exponent += decimals; /* truncate trailing zeroes after decimal point */ @@ -617,6 +684,7 @@ static void UnityPrintExpectedAndActualStringsLen(const char* expected, * Assertion & Control Helpers *-----------------------------------------------*/ +/*-----------------------------------------------*/ static int UnityIsOneArrayNull(UNITY_INTERNAL_PTR expected, UNITY_INTERNAL_PTR actual, const UNITY_LINE_TYPE lineNumber, @@ -649,6 +717,7 @@ static int UnityIsOneArrayNull(UNITY_INTERNAL_PTR expected, * Assertion Functions *-----------------------------------------------*/ +/*-----------------------------------------------*/ void UnityAssertBits(const UNITY_INT mask, const UNITY_INT expected, const UNITY_INT actual, @@ -755,9 +824,15 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected, UnityPrintPointlessAndBail(); } - if (expected == actual) return; /* Both are NULL or same pointer */ + if (expected == actual) + { + return; /* Both are NULL or same pointer */ + } + if (UnityIsOneArrayNull(expected, actual, lineNumber, msg)) + { UNITY_FAIL_AND_BAIL; + } while ((elements > 0) && elements--) { @@ -842,12 +917,14 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected, UnityPrint(UnityStrDelta) #endif /* UNITY_EXCLUDE_FLOAT_PRINT */ +/*-----------------------------------------------*/ static int UnityFloatsWithin(UNITY_FLOAT delta, UNITY_FLOAT expected, UNITY_FLOAT actual) { UNITY_FLOAT diff; UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff); } +/*-----------------------------------------------*/ void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* expected, UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* actual, const UNITY_UINT32 num_elements, @@ -866,9 +943,15 @@ void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* expected, UnityPrintPointlessAndBail(); } - if (expected == actual) return; /* Both are NULL or same pointer */ + if (expected == actual) + { + return; /* Both are NULL or same pointer */ + } + if (UnityIsOneArrayNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg)) + { UNITY_FAIL_AND_BAIL; + } while (elements--) { @@ -953,14 +1036,18 @@ void UnityAssertFloatSpecial(const UNITY_FLOAT actual, UnityTestResultsFailBegin(lineNumber); UnityPrint(UnityStrExpected); if (!should_be_trait) + { UnityPrint(UnityStrNot); + } UnityPrint(trait_names[trait_index]); UnityPrint(UnityStrWas); #ifndef UNITY_EXCLUDE_FLOAT_PRINT UnityPrintFloat((UNITY_DOUBLE)actual); #else if (should_be_trait) + { UnityPrint(UnityStrNot); + } UnityPrint(trait_names[trait_index]); #endif UnityAddMsgIfSpecified(msg); @@ -978,6 +1065,7 @@ static int UnityDoublesWithin(UNITY_DOUBLE delta, UNITY_DOUBLE expected, UNITY_D UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff); } +/*-----------------------------------------------*/ void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* expected, UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* actual, const UNITY_UINT32 num_elements, @@ -996,9 +1084,15 @@ void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* expecte UnityPrintPointlessAndBail(); } - if (expected == actual) return; /* Both are NULL or same pointer */ + if (expected == actual) + { + return; /* Both are NULL or same pointer */ + } + if (UnityIsOneArrayNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg)) + { UNITY_FAIL_AND_BAIL; + } while (elements--) { @@ -1038,7 +1132,6 @@ void UnityAssertDoublesWithin(const UNITY_DOUBLE delta, } /*-----------------------------------------------*/ - void UnityAssertDoubleSpecial(const UNITY_DOUBLE actual, const char* msg, const UNITY_LINE_TYPE lineNumber, @@ -1083,14 +1176,18 @@ void UnityAssertDoubleSpecial(const UNITY_DOUBLE actual, UnityTestResultsFailBegin(lineNumber); UnityPrint(UnityStrExpected); if (!should_be_trait) + { UnityPrint(UnityStrNot); + } UnityPrint(trait_names[trait_index]); UnityPrint(UnityStrWas); #ifndef UNITY_EXCLUDE_FLOAT_PRINT UnityPrintFloat(actual); #else if (should_be_trait) + { UnityPrint(UnityStrNot); + } UnityPrint(trait_names[trait_index]); #endif UnityAddMsgIfSpecified(msg); @@ -1113,16 +1210,24 @@ void UnityAssertNumbersWithin(const UNITY_UINT delta, if ((style & UNITY_DISPLAY_RANGE_INT) == UNITY_DISPLAY_RANGE_INT) { if (actual > expected) - Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(actual - expected) > delta); + { + Unity.CurrentTestFailed = (((UNITY_UINT)actual - (UNITY_UINT)expected) > delta); + } else - Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(expected - actual) > delta); + { + Unity.CurrentTestFailed = (((UNITY_UINT)expected - (UNITY_UINT)actual) > delta); + } } else { if ((UNITY_UINT)actual > (UNITY_UINT)expected) - Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(actual - expected) > delta); + { + Unity.CurrentTestFailed = (((UNITY_UINT)actual - (UNITY_UINT)expected) > delta); + } else - Unity.CurrentTestFailed = (UNITY_UINT)((UNITY_UINT)(expected - actual) > delta); + { + Unity.CurrentTestFailed = (((UNITY_UINT)expected - (UNITY_UINT)actual) > delta); + } } if (Unity.CurrentTestFailed) @@ -1318,9 +1423,15 @@ void UnityAssertEqualMemory(UNITY_INTERNAL_PTR expected, UnityPrintPointlessAndBail(); } - if (expected == actual) return; /* Both are NULL or same pointer */ + if (expected == actual) + { + return; /* Both are NULL or same pointer */ + } + if (UnityIsOneArrayNull(expected, actual, lineNumber, msg)) + { UNITY_FAIL_AND_BAIL; + } while (elements--) { @@ -1397,6 +1508,7 @@ UNITY_INTERNAL_PTR UnityNumToPtr(const UNITY_INT num, const UNITY_UINT8 size) } #ifndef UNITY_EXCLUDE_FLOAT +/*-----------------------------------------------*/ UNITY_INTERNAL_PTR UnityFloatToPtr(const float num) { UnityQuickCompare.f = num; @@ -1405,6 +1517,7 @@ UNITY_INTERNAL_PTR UnityFloatToPtr(const float num) #endif #ifndef UNITY_EXCLUDE_DOUBLE +/*-----------------------------------------------*/ UNITY_INTERNAL_PTR UnityDoubleToPtr(const double num) { UnityQuickCompare.d = num; @@ -1416,6 +1529,7 @@ UNITY_INTERNAL_PTR UnityDoubleToPtr(const double num) * Control Functions *-----------------------------------------------*/ +/*-----------------------------------------------*/ void UnityFail(const char* msg, const UNITY_LINE_TYPE line) { RETURN_IF_FAIL_OR_IGNORE; @@ -1540,6 +1654,7 @@ char* UnityOptionIncludeNamed = NULL; char* UnityOptionExcludeNamed = NULL; int UnityVerbosity = 1; +/*-----------------------------------------------*/ int UnityParseOptions(int argc, char** argv) { UnityOptionIncludeNamed = NULL; @@ -1556,9 +1671,13 @@ int UnityParseOptions(int argc, char** argv) case 'n': /* include tests with name including this string */ case 'f': /* an alias for -n */ if (argv[i][2] == '=') + { UnityOptionIncludeNamed = &argv[i][3]; + } else if (++i < argc) + { UnityOptionIncludeNamed = argv[i]; + } else { UnityPrint("ERROR: No Test String to Include Matches For"); @@ -1574,9 +1693,13 @@ int UnityParseOptions(int argc, char** argv) break; case 'x': /* exclude tests with name including this string */ if (argv[i][2] == '=') + { UnityOptionExcludeNamed = &argv[i][3]; + } else if (++i < argc) + { UnityOptionExcludeNamed = argv[i]; + } else { UnityPrint("ERROR: No Test String to Exclude Matches For"); @@ -1596,6 +1719,7 @@ int UnityParseOptions(int argc, char** argv) return 0; } +/*-----------------------------------------------*/ int IsStringInBiggerString(const char* longstring, const char* shortstring) { const char* lptr = longstring; @@ -1603,7 +1727,9 @@ int IsStringInBiggerString(const char* longstring, const char* shortstring) const char* lnext = lptr; if (*sptr == '*') + { return 1; + } while (*lptr) { @@ -1634,9 +1760,11 @@ int IsStringInBiggerString(const char* longstring, const char* shortstring) lptr = lnext; sptr = shortstring; } + return 0; } +/*-----------------------------------------------*/ int UnityStringArgumentMatches(const char* str) { int retval; @@ -1649,7 +1777,9 @@ int UnityStringArgumentMatches(const char* str) while (ptr1[0] != 0) { if ((ptr1[0] == '"') || (ptr1[0] == '\'')) + { ptr1++; + } /* look for the start of the next partial */ ptr2 = ptr1; @@ -1658,26 +1788,37 @@ int UnityStringArgumentMatches(const char* str) { ptr2++; if ((ptr2[0] == ':') && (ptr2[1] != 0) && (ptr2[0] != '\'') && (ptr2[0] != '"') && (ptr2[0] != ',')) + { ptrf = &ptr2[1]; + } } while ((ptr2[0] != 0) && (ptr2[0] != '\'') && (ptr2[0] != '"') && (ptr2[0] != ',')); + while ((ptr2[0] != 0) && ((ptr2[0] == ':') || (ptr2[0] == '\'') || (ptr2[0] == '"') || (ptr2[0] == ','))) + { ptr2++; + } /* done if complete filename match */ retval = IsStringInBiggerString(Unity.TestFile, ptr1); if (retval == 1) + { return retval; + } /* done if testname match after filename partial match */ if ((retval == 2) && (ptrf != 0)) { if (IsStringInBiggerString(Unity.CurrentTestName, ptrf)) + { return 1; + } } /* done if complete testname match */ if (IsStringInBiggerString(Unity.CurrentTestName, ptr1) == 1) + { return 1; + } ptr1 = ptr2; } @@ -1686,6 +1827,7 @@ int UnityStringArgumentMatches(const char* str) return 0; } +/*-----------------------------------------------*/ int UnityTestMatches(void) { /* Check if this test name matches the included test pattern */ @@ -1695,14 +1837,19 @@ int UnityTestMatches(void) retval = UnityStringArgumentMatches(UnityOptionIncludeNamed); } else + { retval = 1; + } /* Check if this test name matches the excluded test pattern */ if (UnityOptionExcludeNamed) { if (UnityStringArgumentMatches(UnityOptionExcludeNamed)) + { retval = 0; + } } + return retval; } diff --git a/src/unity_internals.h b/src/unity_internals.h index 9bdd171ca26db4658652bc12849eefd8378378cb..2a3068230631a1ee296cb08d100ccf4476791712 100644 --- a/src/unity_internals.h +++ b/src/unity_internals.h @@ -363,7 +363,6 @@ typedef UNITY_FLOAT_TYPE UNITY_FLOAT; # undef UNITY_WEAK_PRAGMA #endif - /*------------------------------------------------------- * Internal Structs Needed *-------------------------------------------------------*/ @@ -404,11 +403,13 @@ UNITY_DISPLAY_STYLE_UINT = sizeof(unsigned) + UNITY_DISPLAY_RANGE_UINT, typedef enum { + UNITY_WITHIN = 0, UNITY_EQUAL_TO = 1, UNITY_GREATER_THAN = 2, UNITY_GREATER_OR_EQUAL = 2 + UNITY_EQUAL_TO, UNITY_SMALLER_THAN = 4, - UNITY_SMALLER_OR_EQUAL = 4 + UNITY_EQUAL_TO + UNITY_SMALLER_OR_EQUAL = 4 + UNITY_EQUAL_TO, + UNITY_UNKNOWN } UNITY_COMPARISON_T; #ifndef UNITY_EXCLUDE_FLOAT @@ -429,7 +430,8 @@ typedef enum UNITY_FLOAT_TRAIT typedef enum { UNITY_ARRAY_TO_VAL = 0, - UNITY_ARRAY_TO_ARRAY + UNITY_ARRAY_TO_ARRAY, + UNITY_ARRAY_UNKNOWN } UNITY_FLAGS_T; struct UNITY_STORAGE_T @@ -577,9 +579,9 @@ void UnityAssertNumbersWithin(const UNITY_UINT delta, const UNITY_LINE_TYPE lineNumber, const UNITY_DISPLAY_STYLE_T style); -void UnityFail(const char* msg, const UNITY_LINE_TYPE line); +void UnityFail(const char* message, const UNITY_LINE_TYPE line); -void UnityIgnore(const char* msg, const UNITY_LINE_TYPE line); +void UnityIgnore(const char* message, const UNITY_LINE_TYPE line); #ifndef UNITY_EXCLUDE_FLOAT void UnityAssertFloatsWithin(const UNITY_FLOAT delta, @@ -835,9 +837,9 @@ int UnityTestMatches(void); #define UNITY_TEST_ASSERT_EQUAL_INT64_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT64, UNITY_ARRAY_TO_ARRAY) #define UNITY_TEST_ASSERT_EQUAL_UINT64_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_UINT64, UNITY_ARRAY_TO_ARRAY) #define UNITY_TEST_ASSERT_EQUAL_HEX64_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_HEX64, UNITY_ARRAY_TO_ARRAY) -#define UNITY_TEST_ASSERT_EACH_EQUAL_INT64(expected, actual, num_elements, line, message) UnityAssertEqualIntArray(UnityNumToPtr((UNITY_INT)(UNITY_INT64)expected, 8), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT64, UNITY_ARRAY_TO_VAL) -#define UNITY_TEST_ASSERT_EACH_EQUAL_UINT64(expected, actual, num_elements, line, message) UnityAssertEqualIntArray(UnityNumToPtr((UNITY_INT)(UNITY_UINT64)expected, 8), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_UINT64, UNITY_ARRAY_TO_VAL) -#define UNITY_TEST_ASSERT_EACH_EQUAL_HEX64(expected, actual, num_elements, line, message) UnityAssertEqualIntArray(UnityNumToPtr((UNITY_INT)(UNITY_INT64)expected, 8), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_HEX64, UNITY_ARRAY_TO_VAL) +#define UNITY_TEST_ASSERT_EACH_EQUAL_INT64(expected, actual, num_elements, line, message) UnityAssertEqualIntArray(UnityNumToPtr((UNITY_INT)(UNITY_INT64)(expected), 8), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT64, UNITY_ARRAY_TO_VAL) +#define UNITY_TEST_ASSERT_EACH_EQUAL_UINT64(expected, actual, num_elements, line, message) UnityAssertEqualIntArray(UnityNumToPtr((UNITY_INT)(UNITY_UINT64)(expected), 8), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_UINT64, UNITY_ARRAY_TO_VAL) +#define UNITY_TEST_ASSERT_EACH_EQUAL_HEX64(expected, actual, num_elements, line, message) UnityAssertEqualIntArray(UnityNumToPtr((UNITY_INT)(UNITY_INT64)(expected), 8), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_HEX64, UNITY_ARRAY_TO_VAL) #define UNITY_TEST_ASSERT_INT64_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((delta), (UNITY_INT)(expected), (UNITY_INT)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT64) #define UNITY_TEST_ASSERT_UINT64_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((delta), (UNITY_INT)(expected), (UNITY_INT)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_UINT64) #define UNITY_TEST_ASSERT_HEX64_WITHIN(delta, expected, actual, line, message) UnityAssertNumbersWithin((delta), (UNITY_INT)(expected), (UNITY_INT)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_HEX64) @@ -919,10 +921,10 @@ int UnityTestMatches(void); #define UNITY_TEST_ASSERT_DOUBLE_IS_NOT_NAN(actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrDouble) #define UNITY_TEST_ASSERT_DOUBLE_IS_NOT_DETERMINATE(actual, line, message) UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), UnityStrErrDouble) #else -#define UNITY_TEST_ASSERT_DOUBLE_WITHIN(delta, expected, actual, line, message) UnityAssertDoublesWithin((UNITY_DOUBLE)(delta), (UNITY_DOUBLE)(expected), (UNITY_DOUBLE)(actual), (message), (UNITY_LINE_TYPE)line) -#define UNITY_TEST_ASSERT_EQUAL_DOUBLE(expected, actual, line, message) UNITY_TEST_ASSERT_DOUBLE_WITHIN((UNITY_DOUBLE)(expected) * (UNITY_DOUBLE)UNITY_DOUBLE_PRECISION, (UNITY_DOUBLE)expected, (UNITY_DOUBLE)actual, (UNITY_LINE_TYPE)(line), message) -#define UNITY_TEST_ASSERT_EQUAL_DOUBLE_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualDoubleArray((UNITY_DOUBLE*)(expected), (UNITY_DOUBLE*)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_ARRAY_TO_ARRAY) -#define UNITY_TEST_ASSERT_EACH_EQUAL_DOUBLE(expected, actual, num_elements, line, message) UnityAssertEqualDoubleArray(UnityDoubleToPtr(expected), (UNITY_DOUBLE*)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)line, UNITY_ARRAY_TO_VAL) +#define UNITY_TEST_ASSERT_DOUBLE_WITHIN(delta, expected, actual, line, message) UnityAssertDoublesWithin((UNITY_DOUBLE)(delta), (UNITY_DOUBLE)(expected), (UNITY_DOUBLE)(actual), (message), (UNITY_LINE_TYPE)(line)) +#define UNITY_TEST_ASSERT_EQUAL_DOUBLE(expected, actual, line, message) UNITY_TEST_ASSERT_DOUBLE_WITHIN((UNITY_DOUBLE)(expected) * (UNITY_DOUBLE)UNITY_DOUBLE_PRECISION, (UNITY_DOUBLE)(expected), (UNITY_DOUBLE)(actual), (UNITY_LINE_TYPE)(line), (message)) +#define UNITY_TEST_ASSERT_EQUAL_DOUBLE_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualDoubleArray((UNITY_DOUBLE*)(expected), (UNITY_DOUBLE*)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_ARRAY_TO_ARRAY) +#define UNITY_TEST_ASSERT_EACH_EQUAL_DOUBLE(expected, actual, num_elements, line, message) UnityAssertEqualDoubleArray(UnityDoubleToPtr(expected), (UNITY_DOUBLE*)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_ARRAY_TO_VAL) #define UNITY_TEST_ASSERT_DOUBLE_IS_INF(actual, line, message) UnityAssertDoubleSpecial((UNITY_DOUBLE)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_INF) #define UNITY_TEST_ASSERT_DOUBLE_IS_NEG_INF(actual, line, message) UnityAssertDoubleSpecial((UNITY_DOUBLE)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_NEG_INF) #define UNITY_TEST_ASSERT_DOUBLE_IS_NAN(actual, line, message) UnityAssertDoubleSpecial((UNITY_DOUBLE)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_FLOAT_IS_NAN) diff --git a/test/.rubocop.yml b/test/.rubocop.yml index c074ca9ac8507a7ce95a54fefbe71bcad9c80c0b..3bfe31a9f7494db552edb3dc1c5b193779050e99 100644 --- a/test/.rubocop.yml +++ b/test/.rubocop.yml @@ -18,7 +18,7 @@ Style/HashSyntax: EnforcedStyle: no_mixed_keys # This is disabled because it seems to get confused over nested hashes -Style/AlignHash: +Layout/AlignHash: Enabled: false EnforcedHashRocketStyle: table EnforcedColonStyle: table diff --git a/test/rakefile b/test/rakefile index 4a2f3d2c12cf7c92386c9387e88504a047d48fe1..770feadc6696572dbacce5c2dfe4816c68a066bd 100644 --- a/test/rakefile +++ b/test/rakefile @@ -4,17 +4,16 @@ # [Released under MIT License. Please refer to license.txt for details] # ========================================== -UNITY_ROOT = File.expand_path(File.dirname(__FILE__)) + '/' $verbose = false require 'rake' require 'rake/clean' -require UNITY_ROOT + 'rakefile_helper' +require_relative 'rakefile_helper' require 'rspec/core/rake_task' TEMP_DIRS = [ - File.join(UNITY_ROOT, 'build'), - File.join(UNITY_ROOT, 'sandbox') + File.join(__dir__, 'build'), + File.join(__dir__, 'sandbox') ] TEMP_DIRS.each do |dir| diff --git a/test/rakefile_helper.rb b/test/rakefile_helper.rb index 1fd60c5fdae594722c46c8ffb52a5d2e06474a6b..d4335ec102cbd5a0ef5255196f9f650fc4b6bd22 100644 --- a/test/rakefile_helper.rb +++ b/test/rakefile_helper.rb @@ -6,9 +6,9 @@ require 'yaml' require 'fileutils' -require UNITY_ROOT + '../auto/unity_test_summary' -require UNITY_ROOT + '../auto/generate_test_runner' -require UNITY_ROOT + '../auto/colour_reporter' +require_relative '../auto/unity_test_summary' +require_relative '../auto/generate_test_runner' +require_relative '../auto/colour_reporter' module RakefileHelpers C_EXTENSION = '.c'.freeze @@ -179,7 +179,7 @@ module RakefileHelpers def report_summary summary = UnityTestSummary.new - summary.root = UNITY_ROOT + summary.root = __dir__ results_glob = "#{$cfg['compiler']['build_path']}*.test*" results_glob.tr!('\\', '/') results = Dir[results_glob] diff --git a/test/tests/testparameterized.c b/test/tests/testparameterized.c index 136cd2f4b36540c3b628413a96236a7f46a49ba1..6100834f6f8fbc4ab87ed07664ddcd3bb11a8622 100644 --- a/test/tests/testparameterized.c +++ b/test/tests/testparameterized.c @@ -46,6 +46,14 @@ void flushSpy(void) {} int SetToOneToFailInTearDown; int SetToOneMeanWeAlreadyCheckedThisGuy; +static unsigned NextExpectedStringIndex; +static unsigned NextExpectedCharIndex; + +void suiteSetUp(void) +{ + NextExpectedStringIndex = 0; + NextExpectedCharIndex = 0; +} void setUp(void) { @@ -111,3 +119,56 @@ void test_NormalFailsStillWork(void) TEST_ASSERT_TRUE(0); VERIFY_FAILS_END } + +TEST_CASE(0, "abc") +TEST_CASE(1, "{") +TEST_CASE(2, "}") +TEST_CASE(3, ";") +TEST_CASE(4, "\"quoted\"") +void test_StringsArePreserved(unsigned index, const char * str) +{ + static const char * const expected[] = + { + "abc", + "{", + "}", + ";", + "\"quoted\"" + }; + + /* Ensure that no test cases are skipped by tracking the next expected index */ + TEST_ASSERT_EQUAL_UINT32(NextExpectedStringIndex, index); + TEST_ASSERT_LESS_THAN(sizeof(expected) / sizeof(expected[0]), index); + TEST_ASSERT_EQUAL_STRING(expected[index], str); + + NextExpectedStringIndex++; +} + +TEST_CASE(0, 'x') +TEST_CASE(1, '{') +TEST_CASE(2, '}') +TEST_CASE(3, ';') +TEST_CASE(4, '\'') +TEST_CASE(5, '"') +void test_CharsArePreserved(unsigned index, char c) +{ + static const char expected[] = + { + 'x', + '{', + '}', + ';', + '\'', + '"' + }; + + /* Ensure that no test cases are skipped by tracking the next expected index */ + TEST_ASSERT_EQUAL_UINT32(NextExpectedCharIndex, index); + TEST_ASSERT_LESS_THAN(sizeof(expected) / sizeof(expected[0]), index); + TEST_ASSERT_EQUAL(expected[index], c); + + NextExpectedCharIndex++; +} + + + diff --git a/test/tests/testunity.c b/test/tests/testunity.c index 7ecb4592f196942f1ea28de2881b792da89de031..366deba3a5f61c57df354ef9da5e66bc7eaca1a9 100644 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -4497,49 +4497,69 @@ void testNotEqualFloatEachEqualLengthZero(void) void testFloatPrinting(void) { -#if defined(UNITY_EXCLUDE_FLOAT_PRINT) || !defined(USING_OUTPUT_SPY) - TEST_IGNORE(); -#else - TEST_ASSERT_EQUAL_PRINT_FLOATING("0", 0.0f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("4.99e-07", 0.000000499f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("5e-07", 0.00000050000005f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("0.100469", 0.100469499f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("1", 0.9999995f); /*Rounding to int place*/ - TEST_ASSERT_EQUAL_PRINT_FLOATING("1", 1.0f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("1.25", 1.25f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("7.99999", 7.99999f); /*Not rounding*/ - TEST_ASSERT_EQUAL_PRINT_FLOATING("16.0002", 16.0002f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("16.0004", 16.0004f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("16.0006", 16.0006f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("999999", 999999.0f); /*Last full print integer*/ - - TEST_ASSERT_EQUAL_PRINT_FLOATING("-0", -0.0f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("-4.99e-07", -0.000000499f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("-5e-07", -0.00000050000005f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("-0.100469", -0.100469499f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("-1", -0.9999995f); /*Rounding to int place*/ - TEST_ASSERT_EQUAL_PRINT_FLOATING("-1", -1.0f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("-1.25", -1.25f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("-7.99999", -7.99999f); /*Not rounding*/ - TEST_ASSERT_EQUAL_PRINT_FLOATING("-16.0002", -16.0002f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("-16.0004", -16.0004f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("-16.0006", -16.0006f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("-999999", -999999.0f); /*Last full print integer*/ - - TEST_ASSERT_EQUAL_PRINT_FLOATING("4.29497e+09", 4294967296.0f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("5e+09", 5000000000.0f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("8e+09", 8.0e+09f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("8.31e+09", 8309999104.0f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("1e+10", 1.0e+10f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("1e+10", 10000000000.0f); +#if defined(UNITY_EXCLUDE_FLOAT_PRINT) || defined(UNITY_INCLUDE_DOUBLE) || !defined(USING_OUTPUT_SPY) + TEST_IGNORE(); +#else + TEST_ASSERT_EQUAL_PRINT_FLOATING("0", 0.0f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("4.99e-07", 0.000000499f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("0.1004695", 0.100469499f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("2", 1.9999995f); /*Rounding to int place*/ + TEST_ASSERT_EQUAL_PRINT_FLOATING("1", 1.0f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("1.25", 1.25f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("7.999999", 7.999999f); /*Not rounding*/ + TEST_ASSERT_EQUAL_PRINT_FLOATING("16.00002", 16.00002f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("16.00004", 16.00004f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("16.00006", 16.00006f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("9999999", 9999999.0f); /*Last full print integer*/ + + TEST_ASSERT_EQUAL_PRINT_FLOATING("-0", -0.0f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-4.99e-07", -0.000000499f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-0.1004695", -0.100469499f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-2", -1.9999995f); /*Rounding to int place*/ + TEST_ASSERT_EQUAL_PRINT_FLOATING("-1", -1.0f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-1.25", -1.25f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-7.999999", -7.999999f); /*Not rounding*/ + TEST_ASSERT_EQUAL_PRINT_FLOATING("-16.00002", -16.00002f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-16.00004", -16.00004f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-16.00006", -16.00006f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-9999999", -9999999.0f); /*Last full print integer*/ + + /* Fails, prints "4.294968e+09" due to FP math imprecision + * TEST_ASSERT_EQUAL_PRINT_FLOATING("4.294967e+09", 4294967296.0f); */ + TEST_ASSERT_EQUAL_PRINT_FLOATING("5e+09", 5000000000.0f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("8e+09", 8.0e+09f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("8.309999e+09", 8309999104.0f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("1e+10", 1.0e+10f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("1e+10", 10000000000.0f); /* Some compilers have trouble with inexact float constants, a float cast works generally */ - TEST_ASSERT_EQUAL_PRINT_FLOATING("1.00005e+10", (float)1.000054e+10f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("1.1e+38", (float)1.10000005e+38f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("1.6353e+10", 1.63529943e+10f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("3.40282e+38", 3.40282346638e38f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("1.000055e+10", (float)1.000055e+10f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("1.1e+38", (float)1.10000005e+38f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("1.635299e+10", 1.63529943e+10f); + /* Fails, prints "3.402824e+38" due to FP math imprecision + * TEST_ASSERT_EQUAL_PRINT_FLOATING("3.402823e+38", 3.40282346638e38f); */ + + TEST_ASSERT_EQUAL_PRINT_FLOATING("-1e+10", -1.0e+10f); + /* Fails, prints "-3.402824e+38" due to FP math imprecision + * TEST_ASSERT_EQUAL_PRINT_FLOATING("-3.402823e+38", -3.40282346638e38f); */ +#endif +} - TEST_ASSERT_EQUAL_PRINT_FLOATING("-1e+10", -1.0e+10f); - TEST_ASSERT_EQUAL_PRINT_FLOATING("-3.40282e+38", -3.40282346638e38f); +void testFloatPrintingRoundTiesToEven(void) +{ +#if defined(UNITY_EXCLUDE_FLOAT_PRINT) || defined(UNITY_INCLUDE_DOUBLE) || !defined(USING_OUTPUT_SPY) + TEST_IGNORE(); +#else + #ifdef UNITY_ROUND_TIES_AWAY_FROM_ZERO + TEST_ASSERT_EQUAL_PRINT_FLOATING("0.0004882813", 0.00048828125f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("488281.3", 488281.25f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("5.000001e-07", 0.00000050000005f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-5.000001e-07", -0.00000050000005f); + #else /* Default to Round ties to even */ + TEST_ASSERT_EQUAL_PRINT_FLOATING("0.0004882812", 0.00048828125f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("488281.2", 488281.25f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("5e-07", 0.00000050000005f); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-5e-07", -0.00000050000005f); + #endif #endif } @@ -4556,37 +4576,84 @@ void testFloatPrintingInfinityAndNaN(void) } #if defined(UNITY_TEST_ALL_FLOATS_PRINT_OK) && defined(USING_OUTPUT_SPY) +#ifdef UNITY_INCLUDE_DOUBLE +static void printFloatValue(float f) +{ + char expected[18]; + + startPutcharSpy(); + UnityPrintFloat(f); + + sprintf(expected, "%.9g", f); + /* We print all NaN's as "nan", not "-nan" */ + if (strcmp(expected, "-nan") == 0) strcpy(expected, "nan"); + + if (strcmp(expected, getBufferPutcharSpy())) + { + /* Fail with diagnostic printing */ + TEST_ASSERT_EQUAL_PRINT_FLOATING(expected, f); + } +} +#else static void printFloatValue(float f) { char expected[18]; char expected_lower[18]; + char expected_lower2[18]; + char expected_lower3[18]; char expected_higher[18]; + char expected_higher2[18]; + char expected_higher3[18]; startPutcharSpy(); - UnityPrintFloat(f); - sprintf(expected, "%.6g", f); - + sprintf(expected, "%.7g", f); /* We print all NaN's as "nan", not "-nan" */ - if(strcmp(expected, "-nan") == 0) strcpy(expected, "nan"); + if (strcmp(expected, "-nan") == 0) strcpy(expected, "nan"); + + strcpy(expected_lower, expected); + strcpy(expected_lower2, expected); + strcpy(expected_lower3, expected); + strcpy(expected_higher, expected); + strcpy(expected_higher2, expected); + strcpy(expected_higher3, expected); - /* Allow for rounding differences in last digit */ - double lower = (double)f * 0.9999995; - double higher = (double)f * 1.0000005; + /* Allow for rounding differences in the last digit */ + double lower = (double)f * 0.99999995; + double higher = (double)f * 1.00000005; - if (isfinite(lower)) sprintf(expected_lower, "%.6g", lower); else strcpy(expected_lower, expected); - if (isfinite(higher)) sprintf(expected_higher, "%.6g", higher); else strcpy(expected_higher, expected); + if(isfinite(lower)) sprintf(expected_lower, "%.7g", lower); + if(isfinite(higher)) sprintf(expected_higher, "%.7g", higher); + + /* Outside [1,10000000] allow for relative error of +/-2.5e-7 */ + if (f < 1.0 || f > 10000000) + { + double lower2 = (double)f * 0.99999985; + double lower3 = (double)f * 0.99999975; + double higher2 = (double)f * 1.00000015; + double higher3 = (double)f * 1.00000025; + + if (isfinite(lower2)) sprintf(expected_lower2, "%.7g", lower2); + if (isfinite(lower3)) sprintf(expected_lower3, "%.7g", lower3); + if (isfinite(higher2)) sprintf(expected_higher2, "%.7g", higher2); + if (isfinite(higher3)) sprintf(expected_higher3, "%.7g", higher3); + } if (strcmp(expected, getBufferPutcharSpy()) != 0 && strcmp(expected_lower, getBufferPutcharSpy()) != 0 && - strcmp(expected_higher, getBufferPutcharSpy()) != 0) + strcmp(expected_lower2, getBufferPutcharSpy()) != 0 && + strcmp(expected_lower3, getBufferPutcharSpy()) != 0 && + strcmp(expected_higher, getBufferPutcharSpy()) != 0 && + strcmp(expected_higher2, getBufferPutcharSpy()) != 0 && + strcmp(expected_higher3, getBufferPutcharSpy()) != 0) { /* Fail with diagnostic printing */ TEST_ASSERT_EQUAL_PRINT_FLOATING(expected, f); } } #endif +#endif void testFloatPrintingRandomSamples(void) { @@ -5286,20 +5353,60 @@ void testDoublePrinting(void) #if defined(UNITY_EXCLUDE_FLOAT_PRINT) || defined(UNITY_EXCLUDE_DOUBLE) || !defined(USING_OUTPUT_SPY) TEST_IGNORE(); #else - TEST_ASSERT_EQUAL_PRINT_FLOATING("0.100469", 0.10046949999999999); - TEST_ASSERT_EQUAL_PRINT_FLOATING("4.29497e+09", 4294967295.999999); - TEST_ASSERT_EQUAL_PRINT_FLOATING("4.29497e+09", 4294967295.9999995); - TEST_ASSERT_EQUAL_PRINT_FLOATING("4.29497e+09", 4294967296.0); - TEST_ASSERT_EQUAL_PRINT_FLOATING("1e+10", 9999999995.0); - TEST_ASSERT_EQUAL_PRINT_FLOATING("9.0072e+15", 9007199254740990.0); - TEST_ASSERT_EQUAL_PRINT_FLOATING("7e+100", 7.0e+100); - TEST_ASSERT_EQUAL_PRINT_FLOATING("3e+200", 3.0e+200); - TEST_ASSERT_EQUAL_PRINT_FLOATING("9.23457e+300", 9.23456789e+300); - - TEST_ASSERT_EQUAL_PRINT_FLOATING("-0.100469", -0.10046949999999999); - TEST_ASSERT_EQUAL_PRINT_FLOATING("-4.29497e+09", -4294967295.999999); - TEST_ASSERT_EQUAL_PRINT_FLOATING("-4.29497e+09", -4294967295.9999995); - TEST_ASSERT_EQUAL_PRINT_FLOATING("-7e+100", -7.0e+100); + TEST_ASSERT_EQUAL_PRINT_FLOATING("0", 0.0); + TEST_ASSERT_EQUAL_PRINT_FLOATING("4.99e-07", 0.000000499); + TEST_ASSERT_EQUAL_PRINT_FLOATING("5.0000005e-07", 0.00000050000005); + TEST_ASSERT_EQUAL_PRINT_FLOATING("0.100469499", 0.100469499); + TEST_ASSERT_EQUAL_PRINT_FLOATING("1", 0.9999999995); /*Rounding to int place*/ + TEST_ASSERT_EQUAL_PRINT_FLOATING("1", 1.0); + TEST_ASSERT_EQUAL_PRINT_FLOATING("1.25", 1.25); + TEST_ASSERT_EQUAL_PRINT_FLOATING("7.99999999", 7.99999999); /*Not rounding*/ + TEST_ASSERT_EQUAL_PRINT_FLOATING("16.0000002", 16.0000002); + TEST_ASSERT_EQUAL_PRINT_FLOATING("16.0000004", 16.0000004); + TEST_ASSERT_EQUAL_PRINT_FLOATING("16.0000006", 16.0000006); + TEST_ASSERT_EQUAL_PRINT_FLOATING("999999999", 999999999.0); /*Last full print integer*/ + + TEST_ASSERT_EQUAL_PRINT_FLOATING("-0", -0.0); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-4.99e-07", -0.000000499); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-5.0000005e-07", -0.00000050000005); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-0.100469499", -0.100469499); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-1", -0.9999999995); /*Rounding to int place*/ + TEST_ASSERT_EQUAL_PRINT_FLOATING("-1", -1.0); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-1.25", -1.25); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-7.99999999", -7.99999999); /*Not rounding*/ + TEST_ASSERT_EQUAL_PRINT_FLOATING("-16.0000002", -16.0000002); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-16.0000004", -16.0000004); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-16.0000006", -16.0000006); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-999999999", -999999999.0); /*Last full print integer*/ + + TEST_ASSERT_EQUAL_PRINT_FLOATING("0.1004695", 0.10046949999999999); + TEST_ASSERT_EQUAL_PRINT_FLOATING("4.2949673e+09", 4294967295.9); + TEST_ASSERT_EQUAL_PRINT_FLOATING("4.2949673e+09", 4294967296.0); + TEST_ASSERT_EQUAL_PRINT_FLOATING("1e+10", 9999999995.0); + TEST_ASSERT_EQUAL_PRINT_FLOATING("9.00719925e+15", 9007199254740990.0); + TEST_ASSERT_EQUAL_PRINT_FLOATING("7e+100", 7.0e+100); + TEST_ASSERT_EQUAL_PRINT_FLOATING("3e+200", 3.0e+200); + TEST_ASSERT_EQUAL_PRINT_FLOATING("9.23456789e+300", 9.23456789e+300); + + TEST_ASSERT_EQUAL_PRINT_FLOATING("-0.1004695", -0.10046949999999999); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-4.2949673e+09", -4294967295.9); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-4.2949673e+09", -4294967296.0); + TEST_ASSERT_EQUAL_PRINT_FLOATING("-7e+100", -7.0e+100); +#endif +} + +void testDoublePrintingRoundTiesToEven(void) +{ +#if defined(UNITY_EXCLUDE_FLOAT_PRINT) || defined(UNITY_EXCLUDE_DOUBLE) || !defined(USING_OUTPUT_SPY) + TEST_IGNORE(); +#else + #ifdef UNITY_ROUND_TIES_AWAY_FROM_ZERO + TEST_ASSERT_EQUAL_PRINT_FLOATING("1.00000001e+10", 10000000050.0); + TEST_ASSERT_EQUAL_PRINT_FLOATING("9.00719925e+15", 9007199245000000.0); + #else /* Default to Round ties to even */ + TEST_ASSERT_EQUAL_PRINT_FLOATING("1e+10", 10000000050.0); + TEST_ASSERT_EQUAL_PRINT_FLOATING("9.00719924e+15", 9007199245000000.0); + #endif #endif }