Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fixed nullptr and made some general test speedups #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

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

### Changed
- `CiConfig::allowable_unittest_files` now uses `Pathname` to full effect
- `nullptr` now defined in its own class

### Deprecated

### Removed

### Fixed
- Assertions on `nullptr`
- The defintion of `nullptr`

### Security

Expand Down
3 changes: 2 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ To speed up testing by targeting only the files you're working on, you can set s
* `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).
* `ARDUINO_CI_SKIP_RUBY_RSPEC_TESTS`: if set, this will skip all tests against ruby code (useful if you are not changing Ruby code).
* `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).
* `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)

You can set them to any value, they just have to be set. Example usage:
Example usage:

```shell
ARDUINO_CI_SKIP_RUBY_RSPEC_TESTS=1 bundle exec rspec
Expand Down
22 changes: 22 additions & 0 deletions SampleProjects/TestSomething/test/null.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,30 @@ unittest(nothing)
unittest(nullpointer)
{
int* myPointer = NULL;
int **notNullPointer = &myPointer;

assertNull(myPointer);
assertNull(nullptr);
assertEqual(myPointer, nullptr);
assertNotEqual(nullptr, notNullPointer);
assertNotNull(notNullPointer);
}

unittest(nullpointer_equal)
{
int* myPointer = NULL;
int **notNullPointer = &myPointer;
assertEqual(nullptr, myPointer);
assertNotEqual(nullptr, notNullPointer);

assertLessOrEqual(nullptr, myPointer);
assertMoreOrEqual(myPointer, nullptr);
assertLessOrEqual(nullptr, notNullPointer);
assertMoreOrEqual(notNullPointer, nullptr);
assertLessOrEqual(myPointer, nullptr);
assertMoreOrEqual(notNullPointer, nullptr);
assertLess(nullptr, notNullPointer);
assertMore(notNullPointer, nullptr);
}

unittest_main()
5 changes: 1 addition & 4 deletions cpp/arduino/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Where possible, variable names from the Arduino library are used to avoid confli
#include "Stream.h"
#include "HardwareSerial.h"
#include "SPI.h"
#include "Nullptr.h"

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


// Define C++11 nullptr
#define nullptr (std::nullptr_t)NULL


7 changes: 7 additions & 0 deletions cpp/arduino/Nullptr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

// Define C++11 nullptr
typedef void * my_nullptr_t;
#define nullptr (my_nullptr_t)NULL

inline std::ostream& operator << (std::ostream& out, const my_nullptr_t &np) { return out << "nullptr"; }
2 changes: 2 additions & 0 deletions cpp/unittest/Assertion.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#define assertTrue(arg) assertEqual(true, arg)
#define assertFalse(arg) assertEqual(false, arg)
#define assertNull(arg) assertEqual((void*)NULL, (void*)arg)
#define assertNotNull(arg) assertNotEqual((void*)NULL, (void*)arg)

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

26 changes: 19 additions & 7 deletions cpp/unittest/Compare.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <avr/pgmspace.h>
#include <WString.h>
#include <Nullptr.h>

template < typename A, typename B > struct Compare
{
Expand Down Expand Up @@ -897,10 +898,21 @@ template < size_t N, size_t M > struct Compare<char [N],char [M]>
return between(a,b) >= 0;
} // moreOrEqual
};
template <typename A, typename B> int compareBetween(const A &a, const B &b) { return Compare<A,B>::between(a,b); }
template <typename A, typename B> bool compareEqual(const A &a, const B &b) { return Compare<A,B>::equal(a,b); }
template <typename A, typename B> bool compareNotEqual(const A &a, const B &b) { return Compare<A,B>::notEqual(a,b); }
template <typename A, typename B> bool compareLess(const A &a, const B &b) { return Compare<A,B>::less(a,b); }
template <typename A, typename B> bool compareMore(const A &a, const B &b) { return Compare<A,B>::more(a,b); }
template <typename A, typename B> bool compareLessOrEqual(const A &a, const B &b) { return Compare<A,B>::lessOrEqual(a,b); }
template <typename A, typename B> bool compareMoreOrEqual(const A &a, const B &b) { return Compare<A,B>::moreOrEqual(a,b); }

// null pointer comparisons
template <typename B> int compareBetween( const my_nullptr_t &a, const B &b) { return Compare<my_nullptr_t,B>::between( a, b); }
template <typename B> bool compareEqual( const my_nullptr_t &a, const B &b) { return Compare<my_nullptr_t,B>::equal( a, b); }
template <typename B> bool compareNotEqual( const my_nullptr_t &a, const B &b) { return Compare<my_nullptr_t,B>::notEqual( a, b); }
template <typename B> bool compareLess( const my_nullptr_t &a, const B &b) { return Compare<my_nullptr_t,B>::less( a, b); }
template <typename B> bool compareMore( const my_nullptr_t &a, const B &b) { return Compare<my_nullptr_t,B>::more( a, b); }
template <typename B> bool compareLessOrEqual(const my_nullptr_t &a, const B &b) { return Compare<my_nullptr_t,B>::lessOrEqual(a, b); }
template <typename B> bool compareMoreOrEqual(const my_nullptr_t &a, const B &b) { return Compare<my_nullptr_t,B>::moreOrEqual(a, b); }

// super general comparisons
template <typename A, typename B> int compareBetween( const A &a, const B &b) { return Compare<A,B>::between( a, b); }
template <typename A, typename B> bool compareEqual( const A &a, const B &b) { return Compare<A,B>::equal( a, b); }
template <typename A, typename B> bool compareNotEqual( const A &a, const B &b) { return Compare<A,B>::notEqual( a, b); }
template <typename A, typename B> bool compareLess( const A &a, const B &b) { return Compare<A,B>::less( a, b); }
template <typename A, typename B> bool compareMore( const A &a, const B &b) { return Compare<A,B>::more( a, b); }
template <typename A, typename B> bool compareLessOrEqual(const A &a, const B &b) { return Compare<A,B>::lessOrEqual(a, b); }
template <typename A, typename B> bool compareMoreOrEqual(const A &a, const B &b) { return Compare<A,B>::moreOrEqual(a, b); }
8 changes: 4 additions & 4 deletions lib/arduino_ci/ci_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,17 @@ def aux_libraries_for_unittest
end

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

ret = paths
unless @unittest_info[:testfiles][:select].nil? || @unittest_info[:testfiles][:select].empty?
ret = ret.select { |p| unittest_info[:testfiles][:select].any? { |glob| File.fnmatch(glob, File.basename(p)) } }
ret.select! { |p| unittest_info[:testfiles][:select].any? { |glob| p.basename.fnmatch(glob) } }
end
unless @unittest_info[:testfiles][:reject].nil?
ret = ret.reject { |p| unittest_info[:testfiles][:reject].any? { |glob| File.fnmatch(glob, File.basename(p)) } }
ret.reject! { |p| unittest_info[:testfiles][:reject].any? { |glob| p.basename.fnmatch(glob) } }
end
ret
end
Expand Down
8 changes: 8 additions & 0 deletions spec/testsomething_unittests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ def get_relative_dir(sampleprojects_tests_dir)
end

test_files = config.allowable_unittest_files(cpp_library.test_files)

# filter the list based on a glob, if provided
unless ENV["ARDUINO_CI_SELECT_CPP_TESTS"].nil?
Dir.chdir(cpp_library.tests_dir) do
globbed = Pathname.glob(ENV["ARDUINO_CI_SELECT_CPP_TESTS"])
test_files.select! { |p| globbed.include?(p.basename) }
end
end
test_files.each do |path|
tfn = File.basename(path)

Expand Down