Skip to content

Commit e68974a

Browse files
authored
Merge pull request #106 from ianfixes/2019-01-30_nullptr_speedups
Fixed nullptr and made some general test speedups
2 parents 48f5023 + 36be6ca commit e68974a

File tree

9 files changed

+71
-16
lines changed

9 files changed

+71
-16
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99
### Added
10+
- Added rspec sensitivity to the environment variable `$ARDUINO_CI_SELECT_CPP_TESTS=<glob>` (for `arduino_ci` gem hackers)
11+
- `assertNotNull()` and `assureNotNull()` C++ comparisons
1012

1113
### Changed
14+
- `CiConfig::allowable_unittest_files` now uses `Pathname` to full effect
15+
- `nullptr` now defined in its own class
1216

1317
### Deprecated
1418

1519
### Removed
1620

1721
### Fixed
22+
- Assertions on `nullptr`
23+
- The defintion of `nullptr`
1824

1925
### Security
2026

CONTRIBUTING.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ To speed up testing by targeting only the files you're working on, you can set s
3232
* `ARDUINO_CI_SKIP_SPLASH_SCREEN_RSPEC_TESTS`: if set, this will avoid any rspec test that calls the arduino executable (and as such, causes the splash screen to pop up).
3333
* `ARDUINO_CI_SKIP_RUBY_RSPEC_TESTS`: if set, this will skip all tests against ruby code (useful if you are not changing Ruby code).
3434
* `ARDUINO_CI_SKIP_CPP_RSPEC_TESTS`: if set, this will skip all tests against the `TestSomething` sample project (useful if you are not changing C++ code).
35+
* `ARDUINO_CI_SELECT_CPP_TESTS=<glob>`: if set, this will skip all C++ unit tests whose filenames don't match the provided glob (executed in the tests directory)
3536

36-
You can set them to any value, they just have to be set. Example usage:
37+
Example usage:
3738

3839
```shell
3940
ARDUINO_CI_SKIP_RUBY_RSPEC_TESTS=1 bundle exec rspec

SampleProjects/TestSomething/test/null.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,30 @@ unittest(nothing)
2222
unittest(nullpointer)
2323
{
2424
int* myPointer = NULL;
25+
int **notNullPointer = &myPointer;
26+
2527
assertNull(myPointer);
2628
assertNull(nullptr);
29+
assertEqual(myPointer, nullptr);
30+
assertNotEqual(nullptr, notNullPointer);
31+
assertNotNull(notNullPointer);
32+
}
33+
34+
unittest(nullpointer_equal)
35+
{
36+
int* myPointer = NULL;
37+
int **notNullPointer = &myPointer;
38+
assertEqual(nullptr, myPointer);
39+
assertNotEqual(nullptr, notNullPointer);
40+
41+
assertLessOrEqual(nullptr, myPointer);
42+
assertMoreOrEqual(myPointer, nullptr);
43+
assertLessOrEqual(nullptr, notNullPointer);
44+
assertMoreOrEqual(notNullPointer, nullptr);
45+
assertLessOrEqual(myPointer, nullptr);
46+
assertMoreOrEqual(notNullPointer, nullptr);
47+
assertLess(nullptr, notNullPointer);
48+
assertMore(notNullPointer, nullptr);
2749
}
2850

2951
unittest_main()

cpp/arduino/Arduino.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Where possible, variable names from the Arduino library are used to avoid confli
1515
#include "Stream.h"
1616
#include "HardwareSerial.h"
1717
#include "SPI.h"
18+
#include "Nullptr.h"
1819

1920
typedef bool boolean;
2021
typedef uint8_t byte;
@@ -72,7 +73,3 @@ inline unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8)
7273
#define word(...) makeWord(__VA_ARGS__)
7374

7475

75-
// Define C++11 nullptr
76-
#define nullptr (std::nullptr_t)NULL
77-
78-

cpp/arduino/Nullptr.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
// Define C++11 nullptr
4+
typedef void * my_nullptr_t;
5+
#define nullptr (my_nullptr_t)NULL
6+
7+
inline std::ostream& operator << (std::ostream& out, const my_nullptr_t &np) { return out << "nullptr"; }

cpp/unittest/Assertion.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#define assertTrue(arg) assertEqual(true, arg)
4040
#define assertFalse(arg) assertEqual(false, arg)
4141
#define assertNull(arg) assertEqual((void*)NULL, (void*)arg)
42+
#define assertNotNull(arg) assertNotEqual((void*)NULL, (void*)arg)
4243

4344
/** macro generates optional output and calls fail() followed by a return if false. */
4445
#define assureEqual(arg1,arg2) assureOp("assureEqual","expected",arg1,compareEqual,"==","actual",arg2)
@@ -50,4 +51,5 @@
5051
#define assureTrue(arg) assureEqual(true, arg)
5152
#define assureFalse(arg) assureEqual(false, arg)
5253
#define assureNull(arg) assureEqual((void*)NULL, (void*)arg)
54+
#define assureNotNull(arg) assureNotEqual((void*)NULL, (void*)arg)
5355

cpp/unittest/Compare.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include <avr/pgmspace.h>
33
#include <WString.h>
4+
#include <Nullptr.h>
45

56
template < typename A, typename B > struct Compare
67
{
@@ -897,10 +898,21 @@ template < size_t N, size_t M > struct Compare<char [N],char [M]>
897898
return between(a,b) >= 0;
898899
} // moreOrEqual
899900
};
900-
template <typename A, typename B> int compareBetween(const A &a, const B &b) { return Compare<A,B>::between(a,b); }
901-
template <typename A, typename B> bool compareEqual(const A &a, const B &b) { return Compare<A,B>::equal(a,b); }
902-
template <typename A, typename B> bool compareNotEqual(const A &a, const B &b) { return Compare<A,B>::notEqual(a,b); }
903-
template <typename A, typename B> bool compareLess(const A &a, const B &b) { return Compare<A,B>::less(a,b); }
904-
template <typename A, typename B> bool compareMore(const A &a, const B &b) { return Compare<A,B>::more(a,b); }
905-
template <typename A, typename B> bool compareLessOrEqual(const A &a, const B &b) { return Compare<A,B>::lessOrEqual(a,b); }
906-
template <typename A, typename B> bool compareMoreOrEqual(const A &a, const B &b) { return Compare<A,B>::moreOrEqual(a,b); }
901+
902+
// null pointer comparisons
903+
template <typename B> int compareBetween( const my_nullptr_t &a, const B &b) { return Compare<my_nullptr_t,B>::between( a, b); }
904+
template <typename B> bool compareEqual( const my_nullptr_t &a, const B &b) { return Compare<my_nullptr_t,B>::equal( a, b); }
905+
template <typename B> bool compareNotEqual( const my_nullptr_t &a, const B &b) { return Compare<my_nullptr_t,B>::notEqual( a, b); }
906+
template <typename B> bool compareLess( const my_nullptr_t &a, const B &b) { return Compare<my_nullptr_t,B>::less( a, b); }
907+
template <typename B> bool compareMore( const my_nullptr_t &a, const B &b) { return Compare<my_nullptr_t,B>::more( a, b); }
908+
template <typename B> bool compareLessOrEqual(const my_nullptr_t &a, const B &b) { return Compare<my_nullptr_t,B>::lessOrEqual(a, b); }
909+
template <typename B> bool compareMoreOrEqual(const my_nullptr_t &a, const B &b) { return Compare<my_nullptr_t,B>::moreOrEqual(a, b); }
910+
911+
// super general comparisons
912+
template <typename A, typename B> int compareBetween( const A &a, const B &b) { return Compare<A,B>::between( a, b); }
913+
template <typename A, typename B> bool compareEqual( const A &a, const B &b) { return Compare<A,B>::equal( a, b); }
914+
template <typename A, typename B> bool compareNotEqual( const A &a, const B &b) { return Compare<A,B>::notEqual( a, b); }
915+
template <typename A, typename B> bool compareLess( const A &a, const B &b) { return Compare<A,B>::less( a, b); }
916+
template <typename A, typename B> bool compareMore( const A &a, const B &b) { return Compare<A,B>::more( a, b); }
917+
template <typename A, typename B> bool compareLessOrEqual(const A &a, const B &b) { return Compare<A,B>::lessOrEqual(a, b); }
918+
template <typename A, typename B> bool compareMoreOrEqual(const A &a, const B &b) { return Compare<A,B>::moreOrEqual(a, b); }

lib/arduino_ci/ci_config.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,17 +287,17 @@ def aux_libraries_for_unittest
287287
end
288288

289289
# Config allows select / reject (aka whitelist / blacklist) criteria. Enforce on a dir
290-
# @param paths [Array<String>] the initial set of test files
291-
# @return [Array<String>] files that match the select/reject criteria
290+
# @param paths [Array<Pathname>] the initial set of test files
291+
# @return [Array<Pathname>] files that match the select/reject criteria
292292
def allowable_unittest_files(paths)
293293
return paths if @unittest_info[:testfiles].nil?
294294

295295
ret = paths
296296
unless @unittest_info[:testfiles][:select].nil? || @unittest_info[:testfiles][:select].empty?
297-
ret = ret.select { |p| unittest_info[:testfiles][:select].any? { |glob| File.fnmatch(glob, File.basename(p)) } }
297+
ret.select! { |p| unittest_info[:testfiles][:select].any? { |glob| p.basename.fnmatch(glob) } }
298298
end
299299
unless @unittest_info[:testfiles][:reject].nil?
300-
ret = ret.reject { |p| unittest_info[:testfiles][:reject].any? { |glob| File.fnmatch(glob, File.basename(p)) } }
300+
ret.reject! { |p| unittest_info[:testfiles][:reject].any? { |glob| p.basename.fnmatch(glob) } }
301301
end
302302
ret
303303
end

spec/testsomething_unittests_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ def get_relative_dir(sampleprojects_tests_dir)
4444
end
4545

4646
test_files = config.allowable_unittest_files(cpp_library.test_files)
47+
48+
# filter the list based on a glob, if provided
49+
unless ENV["ARDUINO_CI_SELECT_CPP_TESTS"].nil?
50+
Dir.chdir(cpp_library.tests_dir) do
51+
globbed = Pathname.glob(ENV["ARDUINO_CI_SELECT_CPP_TESTS"])
52+
test_files.select! { |p| globbed.include?(p.basename) }
53+
end
54+
end
4755
test_files.each do |path|
4856
tfn = File.basename(path)
4957

0 commit comments

Comments
 (0)