Compare commits

...

189 Commits

Author SHA1 Message Date
Jordan Maples [MSFT] b43855631a Merge pull request #861 from JordanMaples/dev/jomaples/lwg3320
implement LWG3320
2020-04-15 10:00:55 -07:00
Jordan Maples 9cb376c050 adding back free functions for [c|cr][begin|end] 2020-04-14 16:57:12 -07:00
Jordan Maples 7341c5d1b5 implement LWG3320 by deleting const_iterator and const_reverse_iterator 2020-04-14 13:51:49 -07:00
Jordan Maples [MSFT] 044849d6fa Merge pull request #823 from RedDwarf69/std_byte_fix
Add missing #include if built with GSL_USE_STD_BYTE=1
2020-04-13 11:33:56 -07:00
Jordan Maples [MSFT] 24dc828cbb Merge pull request #860 from karzhenkov/fix-googletest-link
Fix link to Google Test
2020-04-13 10:35:19 -07:00
Alexander Karzhenkov 8b8c25fa66 Fix link to Google Test 2020-04-13 18:19:51 +05:00
Jordan Maples [MSFT] f7aae78ff6 Merge pull request #854 from JordanMaples/dev/jomaples/LWG3255
Implement span std::array ctor changes from LWG3255
2020-04-09 23:42:04 -07:00
Jordan Maples efbce17ca4 testing one of CaseyCarter's comments 2020-04-09 17:33:33 -07:00
Jordan Maples ffbfcc0a9f apply clang-format to the span_compatibility_tests. 2020-04-09 16:15:08 -07:00
Jordan Maples 5ca02327c4 another test 2020-04-09 15:56:26 -07:00
Jordan Maples 8d907dadfb prevent comparison for apple clang versions older than 11 2020-04-09 14:35:44 -07:00
Jordan Maples b7d9d754ac another attempt at apple clang version filtering 2020-04-09 14:17:52 -07:00
Jordan Maples 809aee2315 testing different major/minor for Apple suppression 2020-04-09 11:51:59 -07:00
Jordan Maples 6ef56d73da fixed a couple typos 2020-04-09 10:35:44 -07:00
Jordan Maples 2f9d873043 added additional filtering to Apple clang versions 9.4 and 10.1 for the ArrayConvertibilityCheck 2020-04-09 10:34:58 -07:00
Jordan Maples f85166aa8b Merge branch 'master' into dev/jomaples/LWG3255 2020-04-09 09:53:45 -07:00
Jordan Maples [MSFT] b0b933b842 Merge pull request #858 from Farwaykorse/patch-1
Solve MSVC warning C4668: __GNUC__ & __clang__ not defined
2020-04-08 23:21:08 -07:00
Jordan Maples [MSFT] bc7ea7aac7 Merge pull request #859 from Farwaykorse/patch-2
Suppress GCC 8 "-Warray-bounds" warning
2020-04-08 23:20:44 -07:00
Farwaykorse 611674d60b GCC "-Warray-bounds" warning persists with release 8.4
modification of commit 5e21831494
2020-04-09 03:50:17 +02:00
Farwaykorse 6e2398b524 Solve MSVC warning C4668: __GNUC__ & __clang__ not defined as a preprocessor macro
MSVC compiler warning C4668:
` '__clang__' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'`
2020-04-09 03:15:57 +02:00
Jordan Maples ff45864dba Merge branch 'dev/jomaples/LWG3255' of https://github.com/JordanMaples/GSL into dev/jomaples/LWG3255 2020-04-08 14:48:05 -07:00
Jordan Maples 15dd17aa5b Merge branch 'master' into dev/jomaples/LWG3255 2020-04-08 14:47:08 -07:00
Jordan Maples [MSFT] c5dd0e6d40 Merge pull request #3 from JordanMaples/dev/jomaples/lwg3255
adding additional filtering
2020-04-08 14:43:15 -07:00
Jordan Maples 01d206f4d8 adding additional filtering 2020-04-08 14:38:01 -07:00
Jordan Maples [MSFT] 226a854a97 Merge pull request #835 from Farwaykorse/TravisCI
Improve CI with TravisCI
2020-03-26 15:00:27 -07:00
Roelf-Jilling 5e21831494 Suppress test for GCC 8.3 triggering "-Warray-bounds" 2020-03-26 20:15:29 +01:00
Roelf-Jilling 04809f8559 TravisCI: moved "based on" comment to relevant legacy section 2020-03-26 17:16:32 +01:00
Roelf-Jilling 511faf07d4 TravisCI: remove deprecated key "sudo" and replace alias "matrix" for "jobs" 2020-03-26 17:04:01 +01:00
Roelf-Jilling 610c40333d TravisCI: add Clang-10 2020-03-26 16:29:27 +01:00
Roelf-Jilling 30c068781f Merge branch 'master' into TravisCI 2020-03-26 16:24:19 +01:00
Roelf-Jilling f4c2292f9d Update to use the latest CMake releases
3.17.0, 3.16.5 and 3.15.7
2020-03-26 16:12:07 +01:00
Jordan Maples [MSFT] 0357a02921 Apply suggestions from code review
Committing suggested change: XCode to Xcode

Co-Authored-By: Kevin Wojniak <kainjow@users.noreply.github.com>
2020-03-23 13:57:46 -07:00
Jordan Maples ddde9e153d update convertibility test 2020-03-19 14:08:04 -07:00
Jordan Maples 6eab19d3c1 moving decl so we dont get the stl_nullptr is not used warning 2020-03-17 15:13:26 -07:00
Jordan Maples f8bcb7d9eb applied clang-format 2020-03-17 15:04:58 -07:00
Jordan Maples 9b3ac8d681 discussed the issue with Casey Carter, the span ctor changes are accurate but the tests are not. The test require work that was done in C++17 regarding qualifier conversions to work correctly. Scoping tests for 17. 2020-03-17 15:02:00 -07:00
Jordan Maples 1dd1320c8b test commit to get extra eyes on the problem 2020-03-17 13:53:13 -07:00
Jordan Maples [MSFT] d90fefea6d Merge pull request #851 from JordanMaples/dev/jomaples/exception
prevent exception include when in no-exception mode.
2020-03-17 13:41:33 -07:00
Jordan Maples 06adf557e9 update kernel mode detection macro 2020-03-12 12:32:32 -07:00
Jordan Maples ce4d689f4f shuffle around code to bring includes to top. 2020-03-06 11:22:09 -08:00
Jordan Maples b30524088a added some comments and ran clang-format 2020-03-06 11:17:58 -08:00
Jordan Maples 769f01900d moving exception include 2020-03-03 17:04:46 -08:00
Jordan Maples [MSFT] 4b643918a1 Merge pull request #784 from hanst99/hunterization
Update "Make library importable by CMake using find_package"
2020-02-26 08:48:04 -08:00
Jordan Maples [MSFT] a3b86971d9 Merge pull request #847 from JordanMaples/dev/jomaples/split_span
Removing non-standard functionality from span header. Use span_ext for span comparisons and make_span functionality.
2020-02-25 16:59:54 -08:00
Jordan Maples [MSFT] 4a6a7a095d add back array header, will remove in separate pr 2020-02-25 14:09:29 -08:00
Jordan Maples [MSFT] d2fdeda06e Merge branch 'dev/jomaples/split_span' of https://github.com/jordanmaples/gsl into dev/jomaples/split_span 2020-02-25 13:53:00 -08:00
Jordan Maples [MSFT] fc54e0d89a just pulling in array header if we detect apple clang 2020-02-25 13:39:47 -08:00
Jordan Maples [MSFT] 5d54cada8e just pulling in array header if we detect apple clang 2020-02-24 15:49:30 -08:00
Jordan Maples [MSFT] 32e5fea6ac test 2020-02-24 14:47:45 -08:00
Jordan Maples [MSFT] 0f60c68ab9 disgusting test to get apple clang to work, dont look at it 2020-02-24 14:41:29 -08:00
Jordan Maples [MSFT] 60372b6468 apple-clang failed again, forcing class instead of struct 2020-02-24 13:43:42 -08:00
Jordan Maples [MSFT] 89271b89c1 fix apple clang forward declaration 2020-02-24 13:06:01 -08:00
Jordan Maples [MSFT] ac37004274 clang defines struct as array where msvc and g++ define it as class. 2020-02-24 11:45:33 -08:00
Jordan Maples [MSFT] 3144082909 forward declare span and remove header 2020-02-24 11:16:26 -08:00
Jordan Maples [MSFT] c853017be3 forgot nl @ eof in span_ext_tests.cpp 2020-02-19 14:44:22 -08:00
Jordan Maples [MSFT] 94f43d4adf splitting up span's standard and non-standard behavior 2020-02-19 14:28:12 -08:00
Jordan Maples [MSFT] 691a78c016 Merge pull request #844 from JordanMaples/dev/jomaples/unsigned
gsl::span: Migrate from signed to unsigned
2020-02-18 16:55:14 -08:00
Jordan Maples [MSFT] 46603698ec rewrite operators in iterator to take advantage of nrvo 2020-02-18 15:36:15 -08:00
Jordan Maples [MSFT] f4c608fd39 addressing comments 2020-02-18 14:09:11 -08:00
Jordan Maples [MSFT] 8ba6cb1074 Merge branch 'dev/jomaples/unsigned' of https://github.com/jordanmaples/gsl into dev/jomaples/unsigned 2020-02-18 10:13:14 -08:00
Jordan Maples [MSFT] 8d6ca323fa GTest latest is breaking, rolling back to 1.10 until further investigation 2020-02-18 10:13:03 -08:00
Jordan Maples [MSFT] 6b01a0488b Update span_compatibility_tests.cpp
some fixes.
2020-02-14 16:25:25 -08:00
jordan maples b6b1e9c3cf assert -> expect_true 2020-02-14 16:13:21 -08:00
Jordan Maples [MSFT] 67a7f7eaef errors in appveyor are indicating that AsWritableByesCompilersFor needs to be static. 2020-02-14 15:44:58 -08:00
Jordan Maples [MSFT] 41ae38f197 addressing a few more comments and adding gsl-std span compatibility tests 2020-02-14 15:24:46 -08:00
Jordan Maples [MSFT] 926aaeca56 reviewed the pr in its entirety and made some slight modifications. Removed all members and fields marked as deprecated. 2020-02-11 16:36:07 -08:00
Jordan Maples [MSFT] cce6ee563e address issue with v140 toolset 2020-02-10 17:09:58 -08:00
Jordan Maples [MSFT] dd78144c2e msvc and gcc work locally 2020-02-10 13:17:22 -08:00
Hannes Steffenhagen 849f14083a Add a note on CMake usage of exported target to the README 2020-02-08 13:11:11 +00:00
Hannes Steffenhagen 9c0c6b246c Add Microsoft.GSL::GSL alias for GSL
This ensures that dependents can be agnostic about
whether GSL was added via `add_subdirectory` or `find_package`
2020-02-08 12:58:25 +00:00
Hannes Steffenhagen 886fc95142 Remove PUBLIC_HEADER attribute from install command
Since we already explicitly install `include/gsl`, this
doesn’t do anything.
2020-02-08 12:55:19 +00:00
Hannes Steffenhagen ebe8844344 Make GSL includes based on CMAKE_INSTALL_INCLUDEDIR
This is to avoid inconsistencies with the `install`
command which specifies this as the destination for includes.
2020-02-08 12:53:40 +00:00
Jordan Maples [MSFT] d8fa68c4a5 address build failures 2020-02-07 14:40:09 -08:00
Jordan Maples [MSFT] b81d6e40e1 constexpr the make_subspan calls. 2020-02-07 14:15:44 -08:00
Jordan Maples [MSFT] 377b2db537 almost parity with std::span 2020-02-07 14:09:57 -08:00
Jordan Maples [MSFT] 202f332702 Merge pull request #845 from cbuchart/master
Fix incorrect package name for vcpkg
2020-02-07 10:41:33 -08:00
cbuchart 3f0d733466 Fix incorrect package name for vcpkg 2020-02-07 15:25:58 +01:00
Jordan Maples [MSFT] a49ff1b8df update span specialization of at, change some tests back to int i with narrowing for the comparisons. 2020-02-05 17:12:31 -08:00
Jordan Maples [MSFT] 3b9d15f49f reverting changes to gsl::index 2020-02-05 17:02:23 -08:00
Jordan Maples [MSFT] 45f016d96f add back capture variable for comparison in span_test 2020-02-05 14:41:41 -08:00
Jordan Maples [MSFT] ff5f7973a2 remove brackets around this deref for span_iterator operators 2020-02-05 14:36:34 -08:00
Jordan Maples [MSFT] 7fcc142ffc addressing travis errors 2020-02-05 14:32:08 -08:00
Jordan Maples [MSFT] 432be4852c captureing result of iterator comparisons to address -Wunused-comparison 2020-02-05 13:59:48 -08:00
Jordan Maples [MSFT] 5cf1610cfe prevent overflow in size_bytes. fix compilation issue for clang 3.6 and 3.7 2020-02-05 12:58:49 -08:00
Jordan Maples [MSFT] d7e1611137 gsl::index ptrdiff_t -> size_t. remove span::at span::operator(). 2020-02-04 16:53:43 -08:00
Jordan Maples [MSFT] 5a1e4f3953 update index_type to size_type, fix a couple Expects 2020-02-04 15:31:33 -08:00
Jordan Maples [MSFT] 61c6ef8d42 remove index_type from string_span, update comments 2020-02-04 13:30:03 -08:00
Jordan Maples [MSFT] db2134485a Merge branch 'dev/jomaples/unsigned' of https://github.com/jordanmaples/gsl into dev/jomaples/unsigned 2020-02-04 12:01:30 -08:00
Jordan Maples [MSFT] 0c6ce424ab refactoring index_type to size_type, changing expects 2020-02-04 12:01:27 -08:00
Jordan Maples [MSFT] e9fea77bcb Update include/gsl/span
Co-Authored-By: Casey Carter <cartec69@gmail.com>
2020-02-03 18:31:50 -08:00
Jordan Maples [MSFT] 4ec7058b56 Update include/gsl/span
Co-Authored-By: Casey Carter <cartec69@gmail.com>
2020-02-03 18:31:21 -08:00
Jordan Maples [MSFT] 49e7ed1ebf updating comparisons 2020-02-03 18:27:09 -08:00
Jordan Maples [MSFT] 592c28c6d1 fixing spaces in gsl_suppress 2020-02-03 18:18:14 -08:00
Jordan Maples [MSFT] 877816faa4 addressing feedback 2020-02-03 18:16:41 -08:00
Jordan Maples [MSFT] 2b7a8f1f2f Merge branch 'dev/jomaples/unsigned' of https://github.com/jordanmaples/gsl into dev/jomaples/unsigned 2020-02-03 18:14:52 -08:00
Jordan Maples [MSFT] e0dc8095b3 addressing feedback 2020-02-03 18:13:14 -08:00
Jordan Maples [MSFT] 81c2a1da15 addressing feedback 2020-02-03 18:12:57 -08:00
Jordan Maples [MSFT] 827fafd32c Update include/gsl/span
Co-Authored-By: Casey Carter <cartec69@gmail.com>
2020-02-03 18:12:32 -08:00
Jordan Maples [MSFT] 24646c6f7c Update include/gsl/span
Co-Authored-By: Casey Carter <cartec69@gmail.com>
2020-02-03 17:51:45 -08:00
Jordan Maples [MSFT] f4ee6ee73b Update include/gsl/span
Co-Authored-By: Casey Carter <cartec69@gmail.com>
2020-02-03 17:51:25 -08:00
Jordan Maples [MSFT] 1815791af8 Change iterator from Span* & ptrdiff_t to pointer, pointer, pointer 2020-02-03 16:26:36 -08:00
Jordan Maples [MSFT] d9d6ea8196 remove comparison to dynamic_extent 2020-02-03 13:53:38 -08:00
Jordan Maples [MSFT] 3e40b3fa4b Merge branch 'dev/jomaples/unsigned' of https://github.com/jordanmaples/gsl into dev/jomaples/unsigned 2020-02-03 13:52:11 -08:00
Jordan Maples [MSFT] d0fdbdbffa fixing suppress spacing 2020-02-03 13:52:00 -08:00
Jordan Maples [MSFT] a430823b43 Update include/gsl/string_span
Co-Authored-By: Casey Carter <cartec69@gmail.com>
2020-02-03 13:47:34 -08:00
Jordan Maples [MSFT] 9bb900e834 Update include/gsl/span
Co-Authored-By: Casey Carter <cartec69@gmail.com>
2020-02-03 13:46:18 -08:00
Jordan Maples [MSFT] 17e372c155 Update include/gsl/span
Co-Authored-By: Casey Carter <cartec69@gmail.com>
2020-02-03 13:45:56 -08:00
Jordan Maples [MSFT] 5b8cf5422b casting -1 for dynamic extent 2020-02-03 12:49:57 -08:00
Jordan Maples [MSFT] eabd9358f0 addressing some comments 2020-02-03 12:46:37 -08:00
Jordan Maples [MSFT] ad71477183 fix build breaks 2020-02-03 12:26:15 -08:00
Jordan Maples [MSFT] c31593dd0d updating from test loop from int to size_t 2020-02-03 11:01:24 -08:00
Jordan Maples [MSFT] 8579165d0a change span from signed to unsigned to align to std 2020-02-03 10:56:31 -08:00
Jordan Maples [MSFT] b4dd39615a Merge pull request #842 from JordanMaples/dev/jomaples/not_null
remove move constructor for not_null.
2020-01-30 19:51:35 -08:00
Jordan Maples [MSFT] 6b23937baf remove move constructor for not_null. 2020-01-30 17:00:13 -08:00
Jordan Maples [MSFT] 6e20bfbba2 Merge pull request #836 from Farwaykorse/suppressionToCMake
Compiler warning suppression for tests to CMake
2020-01-22 11:59:51 -08:00
Jordan Maples [MSFT] 57dd11b20f Merge pull request #840 from NancyLi1013/vcpkg-instructins
Add vcpkg installation instructions
2020-01-22 11:52:47 -08:00
NancyLi1013 c61b1503ad Add vcpkg installation instructions 2020-01-18 23:12:43 -08:00
Jordan Maples [MSFT] 7ba7f87475 Merge pull request #837 from Farwaykorse/fix/disable_exceptions
Restore disabling of exceptions for MSVC in "no exception tests"
2020-01-17 09:13:44 -08:00
Jordan Maples [MSFT] d976d41494 Merge pull request #838 from kunaltyagi/patch-1
Fix spelling error in docs
2020-01-17 09:13:09 -08:00
Kunal Tyagi 26a68c882b Correct spelling error in docs 2020-01-17 14:32:37 +09:00
Roelf-Jilling 95730a80c4 Restore disabling of exceptions for Microsoft STL in "no exception tests" 2020-01-16 19:31:31 +01:00
Roelf-Jilling 72312b6474 Revert "adding /EHsc for msvc cmake target gsl_test_config_noexcept"
This reverts commit 3539bd8008.
2020-01-16 19:29:36 +01:00
Roelf-Jilling adc5fb0d14 TravisCI: use more bash variable checks
To detect typos and logic errors.
2020-01-16 17:55:58 +01:00
Roelf-Jilling b29a18d3a8 TravisCI: use stages to end run when newest compilers fail
These tend to have the best error messages.
2020-01-16 17:55:58 +01:00
Roelf-Jilling 5082a21b04 TravisCI: keep googletest directories between CMake generator runs 2020-01-16 17:55:58 +01:00
Roelf-Jilling 8edc449efb CMake 3.2.3 or later to use AppleClang on OSX
GoogleTest uses CMake compile feature detection.
2020-01-16 17:55:58 +01:00
Roelf-Jilling 5caf336e02 TravisCI: validate CMake configuration with all supported releases 2020-01-16 17:55:58 +01:00
Roelf-Jilling ecff9e623c TravisCI: performance - use the default CMake version on CI images 2020-01-16 17:55:58 +01:00
Roelf-Jilling f828c55ec7 TravisCI: add Clang-8 and 9 2020-01-16 17:55:58 +01:00
Roelf-Jilling 586c4e52f8 TravisCI: add GCC-8 and 9 2020-01-16 17:55:58 +01:00
Roelf-Jilling e1a37d0430 Suppress -Wundefined-func-template for C++17 Clang v5.0.x and AppleClang 9.1
Warning appears on v5.0.2 from apt.llvm.org and Ubuntu provided v5.0.1
2020-01-16 17:55:58 +01:00
Roelf-Jilling f0061bfa2a TravisCI: default to Ubuntu Bionic and use Xenial where needed 2020-01-16 17:55:58 +01:00
Roelf-Jilling 8655dc1583 TravisCI: Xcode 9.0 earliest with C++17 support 2020-01-16 17:55:56 +01:00
Roelf-Jilling d768179ff6 TravisCI: add XCode v9.4, 10.1, 10.3 and 11.3 2020-01-16 17:54:59 +01:00
Roelf-Jilling 81f56796a1 MSVC and CoreChecker warning suppression for tests to CMake
Suppress CppCoreCheck warnings on GTest macros.
Suppress CppCoreCheck advice to use the GSL
2020-01-16 11:49:15 +01:00
Roelf-Jilling 4cbd894812 Enable more warnings in GCC 2020-01-16 11:49:15 +01:00
Roelf-Jilling d08ff53e61 Use [[maybe_unused]] with C++17 2020-01-16 11:49:15 +01:00
Roelf-Jilling 186f5d21c1 Move compiler warning suppression for tests from cpp files to CMake 2020-01-16 11:49:00 +01:00
Roelf-Jilling cdedae897c TravisCI: remove redundant setting of CXX variable 2020-01-15 21:06:14 +01:00
Jordan Maples [MSFT] 888b9b9723 Merge pull request #830 from JordanMaples/dev/jomaples/add_missing_span_functions
Add span::front and span::back. See #828.
2020-01-10 10:20:47 -08:00
Jordan Maples [MSFT] 395a5bf550 Merge branch 'master' into dev/jomaples/add_missing_span_functions 2020-01-10 10:20:37 -08:00
Jordan Maples [MSFT] 96b6964b30 Merge pull request #829 from JordanMaples/dev/jomaples/deprecate_span_non_std_functions
Deprecate span::at & span::operator(). See #828.
2020-01-10 10:19:32 -08:00
Jordan Maples [MSFT] a1e6fcbc46 Merge branch 'master' into dev/jomaples/deprecate_span_non_std_functions 2020-01-09 16:39:24 -08:00
Jordan Maples [MSFT] 2d878b94ee Merge branch 'master' into dev/jomaples/add_missing_span_functions 2020-01-09 16:37:12 -08:00
Jordan Maples [MSFT] b076205338 Merge branch 'dev/jomaples/deprecate_span_non_std_functions' of https://github.com/JordanMaples/GSL into dev/jomaples/deprecate_span_non_std_functions 2020-01-09 14:40:08 -08:00
Jordan Maples [MSFT] 75936c3109 missed from the merge 2020-01-09 12:56:00 -08:00
Jordan Maples [MSFT] cfd82aef4e deprecating span::at and span::operator() 2020-01-09 12:51:28 -08:00
Jordan Maples [MSFT] 3c0b38b777 Merge pull request #833 from JordanMaples/dev/jomaples/noexceptspan
Update noexcept in span and span_iterator
2020-01-09 12:45:17 -08:00
Jordan Maples [MSFT] 2bc6a7cb12 Merge pull request #825 from omartijn/free_standing_begin_end_ssize
Implement free-standing (c|r|cr)(begin|end) and ssize functions
2020-01-09 12:32:31 -08:00
Jordan Maples [MSFT] c7f9b3301a noexcepting new functions 2020-01-09 12:13:12 -08:00
Jordan Maples [MSFT] 6eccc81c5f Merge branch 'dev/jomaples/add_missing_span_functions' of https://github.com/JordanMaples/GSL into dev/jomaples/add_missing_span_functions 2020-01-09 12:09:24 -08:00
Jordan Maples [MSFT] 4b823b1651 adding front and back 2020-01-09 12:03:27 -08:00
Jordan Maples [MSFT] 3bf4106a44 noexcept all of the span iterator stuff 2020-01-08 12:34:11 -08:00
Martijn Otto fa8a8117a0 Add tests for free-standing iterator and size functions 2020-01-08 21:02:02 +01:00
Martijn Otto b186b6cc68 Implement free-standing (c|r|cr)(begin|end) and ssize functions 2020-01-08 21:01:52 +01:00
Jordan Maples [MSFT] b33bd4aebe updating all span functions to be noexcept 2020-01-08 11:51:15 -08:00
Jordan Maples [MSFT] 0fefba89da Merge pull request #832 from JordanMaples/dev/jomaples/buildfix
fix appveyor build issues.
2020-01-08 11:47:22 -08:00
Jordan Maples [MSFT] f04907c2f6 added suppression for missing destructor override 2020-01-08 10:13:00 -08:00
Jordan Maples [MSFT] 7786da91c9 Merge pull request #831 from JordanMaples/dev/jomaples/gtest
Migrate from Catch to GoogleTest & always terminate on contract violation
2020-01-07 15:41:25 -08:00
Jordan Maples [MSFT] 40bc3c3f00 testing something 2019-12-20 13:32:14 -08:00
Jordan Maples [MSFT] 8577033f13 addressing comments 2019-12-19 14:05:02 -08:00
Jordan Maples [MSFT] 3539bd8008 adding /EHsc for msvc cmake target gsl_test_config_noexcept 2019-12-13 13:56:42 -08:00
Jordan Maples [MSFT] 17ad4495fb deprecate gsl::details::throw_exception. remove unused header in gsl_assert. gsl_util to use terminate instead of throw_exception 2019-12-13 13:40:25 -08:00
Jordan Maples [MSFT] 6c5c708877 change from string to char* 2019-12-12 16:48:59 -08:00
Jordan Maples [MSFT] 0931262acf appveyor failed to start, sending dummy commit 2019-12-12 14:39:12 -08:00
Jordan Maples [MSFT] 5099e8fa3e removed string_view for string in tests 2019-12-12 11:15:41 -08:00
Jordan Maples [MSFT] 4f6f05d463 changing text in notices & readme. adding terminate handler for tests 2019-12-12 10:55:26 -08:00
Jordan Maples [MSFT] ec05ecd6b1 dummy commit to trigger appveyor 2019-12-10 11:58:42 -08:00
Jordan Maples [MSFT] 0784866608 added back death test, fixed VS2019 test failures 2019-12-09 16:08:27 -08:00
Jordan Maples 9a88262544 fixing suppression of gtest warnings for gcc 2019-12-06 16:03:47 -08:00
Jordan Maples [MSFT] b39a9732f8 attempting to fix the gtest print issues in vs17 2019-12-04 13:46:50 -08:00
Jordan Maples [MSFT] 87eaa45445 suppressing -Wundef in clang (GTest emitted) 2019-12-04 12:48:13 -08:00
Jordan Maples [MSFT] 43aca2646b ignore warnings from gtest 2019-12-04 12:30:58 -08:00
Jordan Maples [MSFT] 603b4671c1 removing unused function 2019-12-03 15:24:52 -08:00
Jordan Maples [MSFT] 2b10729386 gtest migration 2019-12-03 14:32:25 -08:00
Jordan Maples [MSFT] 7004ef506c adding front and back 2019-11-19 16:03:55 -08:00
Jordan Maples [MSFT] 5e0d76a1a7 deprecating span::at and span::operator() 2019-11-19 14:31:21 -08:00
Cristian Morales Vega b213c89700 Add missing #include if built with GSL_USE_STD_BYTE=1
cstddef was being included to detect whether GSL_USE_STD_BYTE should be
set to 0 or 1. But if the user provided GSL_USE_STD_BYTE no detection is
required and the header was never included.
2019-11-08 16:37:23 +00:00
Hannes Steffenhagen 0551cad467 Merge branch 'master' into hunterization 2019-05-09 18:09:21 +01:00
Stefan Reinhold 457fb25609 Merge branch 'hunterization' of github.com:ithron/GSL into hunterization 2018-09-07 17:27:54 +02:00
Stefan Reinhold 8428e083e4 Merge branch 'master' into hunterization 2018-09-07 17:27:31 +02:00
Stefan Reinhold 73022d1103 Merge branch 'master' into hunterization 2018-08-03 00:53:40 +02:00
Stefan Reinhold ffe8ffb452 Dummy commit to force CI re-run 2018-07-18 09:35:36 +02:00
Stefan Reinhold e3ffe1c5cb Fix naming of exported CMake configuration file 2018-07-17 17:56:11 +02:00
Stefan Reinhold e2b57d16f9 Export target GSL in namespace Microsoft.GSL
This is to avoid conflicts with GNU Scientific Library which also has an
exported target named GSL.
2018-07-17 17:43:54 +02:00
Stefan Reinhold 9e1645b990 Do not install .natvis files.
This fixes an issue where CMake complains about INTERFACE_SOURCES
containing a path which is prefixed in source directory on windows.
2018-07-17 13:07:27 +02:00
Stefan Reinhold 0a31a14d09 Fix INSTALL_INTERFACE for exported target
CMake war reporting: 'Target "GSL" INTERFACE_SOURCES property contains
path: "..." which is prefixed in the source directory'.
CMake was confused because the $<BUILD_INTERFACE: ...> had linebreaks
in it.
Putting everything in one line fixed the issue.
2018-07-17 11:56:38 +02:00
Stefan Reinhold 030454e3e9 Fix for Windows: CMake complains about INTERFACE_SOURCES directory 2018-07-17 11:09:58 +02:00
Stefan Reinhold d452c3b061 Make library importable by CMake using find_package 2018-07-17 10:23:44 +02:00
31 changed files with 5978 additions and 4459 deletions
+413 -157
View File
@@ -1,268 +1,519 @@
# Based on https://github.com/ldionne/hana/blob/master/.travis.yml
language: cpp
sudo: false
notifications:
email: false
# Use Linux unless specified otherwise
os: linux
dist: trusty
dist: bionic
cache:
directories:
- ${TRAVIS_BUILD_DIR}/deps
matrix:
stages:
- name: Latest # build jobs to run first and always
- name: Validation # run other jobs
- name: Legacy # build with all other supported compilers
jobs:
include:
##########################################################################
# Clang on OSX
# Travis seems to take longer to start OSX instances,
# so leave this first for the overall build to be faster
# Validate CMake configuration
##########################################################################
# XCode 8.3
- env: COMPILER=clang++ BUILD_TYPE=Debug GSL_CXX_STANDARD=14
- name: CMake 3.1.3 - latest
stage: Validation
env: &CMAKE_VERSION_LIST
- CMAKE_VERSION: '"3.17.0 3.16.5 3.15.7 3.14.7 3.13.5 3.12.4 3.11.4 3.10.3 3.9.6 3.8.2 3.7.2 3.6.3 3.5.2 3.4.3 3.3.2 3.2.3 3.1.3"'
- GSL_CXX_STANDARD: 14
addons: # Get latest release (candidate)
apt:
sources:
- sourceline: 'deb https://apt.kitware.com/ubuntu/ bionic main'
key_url: 'https://apt.kitware.com/keys/kitware-archive-latest.asc'
- sourceline: 'deb https://apt.kitware.com/ubuntu/ bionic-rc main'
packages:
- cmake
script:
- |
cd ./build
( set -eu
for CMAKE in ${CMAKE_path[@]}; do test_CMake_generate $CMAKE; done
export CXX=clang++
for CMAKE in ${CMAKE_path[@]}; do test_CMake_generate $CMAKE; done
)
- name: CMake 3.2.3 - 3.17.0
stage: Validation
os: osx
osx_image: xcode11.3
env:
- CMAKE_VERSION: '"3.17.0 3.16.5 3.15.7 3.14.7 3.13.5 3.12.4 3.11.4 3.10.3 3.9.6 3.8.2 3.7.2 3.6.3 3.5.2 3.4.3 3.3.2 3.2.3"'
script:
- |
cd ./build
( set -eu
for CMAKE in ${CMAKE_path[@]}; do test_CMake_generate $CMAKE; done
)
##########################################################################
# AppleClang on OSX
##########################################################################
# Xcode 8.3
- name: AppleClang Xcode-8.3 C++14 Debug
stage: Legacy
env: BUILD_TYPE=Debug GSL_CXX_STANDARD=14
os: osx
osx_image: xcode8.3
compiler: clang
- env: COMPILER=clang++ BUILD_TYPE=Release GSL_CXX_STANDARD=14
- name: AppleClang Xcode-8.3 C++14 Release
env: BUILD_TYPE=Release GSL_CXX_STANDARD=14
os: osx
osx_image: xcode8.3
compiler: clang
# XCode 9.1
- env: COMPILER=clang++ BUILD_TYPE=Debug GSL_CXX_STANDARD=14
# Xcode 9.0 earliest C++17 support
- name: AppleClang Xcode-9.0 C++17 Debug
env: BUILD_TYPE=Debug GSL_CXX_STANDARD=17
os: osx
osx_image: xcode9.1
compiler: clang
osx_image: xcode9 # AppleClang 9.1.0 same compiler in Xcode 9.0, 9.1 and 9.2
- name: AppleClang Xcode-9.0 C++17 Release
env: BUILD_TYPE=Release GSL_CXX_STANDARD=17
os: osx
osx_image: xcode9
- name: AppleClang Xcode-9.0 C++14 Debug
env: BUILD_TYPE=Debug GSL_CXX_STANDARD=14
os: osx
osx_image: xcode9
- name: AppleClang Xcode-9.0 C++14 Release
env: BUILD_TYPE=Release GSL_CXX_STANDARD=14
os: osx
osx_image: xcode9
- env: COMPILER=clang++ BUILD_TYPE=Release GSL_CXX_STANDARD=14
# Xcode 9.4
- name: AppleClang Xcode-9.4 C++14 Debug
env: BUILD_TYPE=Debug GSL_CXX_STANDARD=14
os: osx
osx_image: xcode9.1
compiler: clang
osx_image: xcode9.4 # AppleClang 9.1.0 same compiler as Xcode 9.3
- name: AppleClang Xcode-9.4 C++14 Release
env: BUILD_TYPE=Release GSL_CXX_STANDARD=14
os: osx
osx_image: xcode9.4
- name: AppleClang Xcode-9.4 C++17 Debug
env: BUILD_TYPE=Debug GSL_CXX_STANDARD=17
os: osx
osx_image: xcode9.4
- name: AppleClang Xcode-9.4 C++17 Release
env: BUILD_TYPE=Release GSL_CXX_STANDARD=17
os: osx
osx_image: xcode9.4
# Xcode 10.1
- name: AppleClang Xcode-10.1 C++14 Debug
env: BUILD_TYPE=Debug GSL_CXX_STANDARD=14
os: osx
osx_image: xcode10.1 # AppleClang 10.0.0 same compiler as Xcode 10.0
- name: AppleClang Xcode-10.1 C++14 Release
env: BUILD_TYPE=Release GSL_CXX_STANDARD=14
os: osx
osx_image: xcode10.1
- name: AppleClang Xcode-10.1 C++17 Debug
env: BUILD_TYPE=Debug GSL_CXX_STANDARD=17
os: osx
osx_image: xcode10.1
- name: AppleClang Xcode-10.1 C++17 Release
env: BUILD_TYPE=Release GSL_CXX_STANDARD=17
os: osx
osx_image: xcode10.1
# Xcode 10.3
- name: AppleClang Xcode-10.3 C++14 Debug
env: BUILD_TYPE=Debug GSL_CXX_STANDARD=14
os: osx
osx_image: xcode10.3 # AppleClang 10.0.1 same compiler as Xcode 10.2
- name: AppleClang Xcode-10.3 C++14 Release
env: BUILD_TYPE=Release GSL_CXX_STANDARD=14
os: osx
osx_image: xcode10.3
- name: AppleClang Xcode-10.3 C++17 Debug
env: BUILD_TYPE=Debug GSL_CXX_STANDARD=17
os: osx
osx_image: xcode10.3
- name: AppleClang Xcode-10.3 C++17 Release
env: BUILD_TYPE=Release GSL_CXX_STANDARD=17
os: osx
osx_image: xcode10.3
# Xcode 11.3
- name: AppleClang Xcode-11.3 C++17 Debug
stage: Latest
env: BUILD_TYPE=Debug GSL_CXX_STANDARD=17
os: osx
osx_image: xcode11.3 # AppleClang 11.0.0 linker update / same as Xcode 11.2
- name: AppleClang Xcode-11.3 C++17 Release
env: BUILD_TYPE=Release GSL_CXX_STANDARD=17
os: osx
osx_image: xcode11.3
- name: AppleClang Xcode-11.3 C++14 Debug
env: BUILD_TYPE=Debug GSL_CXX_STANDARD=14
os: osx
osx_image: xcode11.3
- name: AppleClang Xcode-11.3 C++14 Release
env: BUILD_TYPE=Release GSL_CXX_STANDARD=14
os: osx
osx_image: xcode11.3
##########################################################################
# Clang on Linux
##########################################################################
# Clang 3.6
- env: COMPILER=clang++-3.6 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
- name: Clang-3.6 C++14 Debug
stage: Legacy
dist: xenial
env: CXX=clang++-3.6 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
addons: &clang36
apt:
packages:
- clang-3.6
- g++-5
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-precise-3.6
- env: COMPILER=clang++-3.6 BUILD_TYPE=Release GSL_CXX_STANDARD=14
- name: Clang-3.6 C++14 Release
dist: xenial
env: CXX=clang++-3.6 BUILD_TYPE=Release GSL_CXX_STANDARD=14
addons: *clang36
# Clang 3.7
- env: COMPILER=clang++-3.7 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
- name: Clang-3.7 C++14 Debug
dist: xenial
env: CXX=clang++-3.7 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
addons: &clang37
apt:
packages:
- clang-3.7
- g++-5
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-precise-3.7
- env: COMPILER=clang++-3.7 BUILD_TYPE=Release GSL_CXX_STANDARD=14
- name: Clang-3.7 C++14 Release
dist: xenial
env: CXX=clang++-3.7 BUILD_TYPE=Release GSL_CXX_STANDARD=14
addons: *clang37
# Clang 3.8
- env: COMPILER=clang++-3.8 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
- name: Clang-3.8 C++14 Debug
dist: xenial
env: CXX=clang++-3.8 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
addons: &clang38
apt:
packages:
- clang-3.8
- g++-5
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-precise-3.8
- env: COMPILER=clang++-3.8 BUILD_TYPE=Release GSL_CXX_STANDARD=14
- name: Clang-3.8 C++14 Release
dist: xenial
env: CXX=clang++-3.8 BUILD_TYPE=Release GSL_CXX_STANDARD=14
addons: *clang38
# Clang 3.9
- env: COMPILER=clang++-3.9 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
- name: Clang-3.9 C++14 Debug
env: CXX=clang++-3.9 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
addons: &clang39
apt:
packages:
- clang-3.9
- g++-5
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-precise-3.9
- env: COMPILER=clang++-3.9 BUILD_TYPE=Release GSL_CXX_STANDARD=14
- name: Clang-3.9 C++14 Release
env: CXX=clang++-3.9 BUILD_TYPE=Release GSL_CXX_STANDARD=14
addons: *clang39
# Clang 4.0
- env: COMPILER=clang++-4.0 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
- name: Clang-4.0 C++14 Debug
env: CXX=clang++-4.0 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
addons: &clang40
apt:
packages:
- clang-4.0
- g++-5
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-4.0
- env: COMPILER=clang++-4.0 BUILD_TYPE=Release GSL_CXX_STANDARD=14
- name: Clang-4.0 C++14 Release
env: CXX=clang++-4.0 BUILD_TYPE=Release GSL_CXX_STANDARD=14
addons: *clang40
# Clang 5.0
- env: COMPILER=clang++-5.0 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
- name: Clang-5.0 C++14 Debug
env: CXX=clang++-5.0 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
addons: &clang50
apt:
packages:
- clang-5.0
- g++-7
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-5.0
- sourceline: 'deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-5.0 main'
key_url: 'https://apt.llvm.org/llvm-snapshot.gpg.key'
- env: COMPILER=clang++-5.0 BUILD_TYPE=Release GSL_CXX_STANDARD=14
- name: Clang-5.0 C++14 Release
env: CXX=clang++-5.0 BUILD_TYPE=Release GSL_CXX_STANDARD=14
addons: *clang50
- env: COMPILER=clang++-5.0 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
- name: Clang-5.0 C++17 Debug
env: CXX=clang++-5.0 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
addons: *clang50
- env: COMPILER=clang++-5.0 BUILD_TYPE=Release GSL_CXX_STANDARD=17
- name: Clang 5.0 C++17 Release
env: CXX=clang++-5.0 BUILD_TYPE=Release GSL_CXX_STANDARD=17
addons: *clang50
# Clang 6.0
- env: COMPILER=clang++-6.0 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
- name: Clang-6.0 C++14 Debug
env: CXX=clang++-6.0 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
addons: &clang60
apt:
packages:
- clang-6.0
- g++-7
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-6.0
- sourceline: 'deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-6.0 main'
key_url: 'https://apt.llvm.org/llvm-snapshot.gpg.key'
- env: COMPILER=clang++-6.0 BUILD_TYPE=Release GSL_CXX_STANDARD=14
- name: Clang 6.0 C++14 Release
env: CXX=clang++-6.0 BUILD_TYPE=Release GSL_CXX_STANDARD=14
addons: *clang60
- name: Clang-6.0 C++17 Debug
env: CXX=clang++-6.0 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
addons: *clang60
- name: Clang 6.0 C++17 Release
env: CXX=clang++-6.0 BUILD_TYPE=Release GSL_CXX_STANDARD=17
addons: *clang60
# Clang 6.0 c++17
- env: COMPILER=clang++-6.0 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
addons: *clang60
# Clang 7 (default on Xenial and Bionic images)
- name: Clang-7 C++14 Debug
env: CXX=clang++ BUILD_TYPE=Debug GSL_CXX_STANDARD=14
- name: Clang-7 C++14 Release
env: CXX=clang++ BUILD_TYPE=Release GSL_CXX_STANDARD=14
- name: Clang-7 C++17 Debug
env: CXX=clang++ BUILD_TYPE=Debug GSL_CXX_STANDARD=17
- name: Clang-7 C++17 Release
env: CXX=clang++ BUILD_TYPE=Release GSL_CXX_STANDARD=17
- env: COMPILER=clang++-6.0 BUILD_TYPE=Release GSL_CXX_STANDARD=17
addons: *clang60
# Clang 7.0
- env: COMPILER=clang++-7 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
addons: &clang70
# Clang 8
- name: Clang-8 C++14 Debug
env: CXX=clang++-8 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
addons: &clang8
apt:
packages:
- clang-7
- g++-7
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-7
- sourceline: 'deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-8 main'
key_url: https://apt.llvm.org/llvm-snapshot.gpg.key
packages:
- clang-8
- name: Clang-8 C++14 Release
env: CXX=clang++-8 BUILD_TYPE=Release GSL_CXX_STANDARD=14
addons: *clang8
- name: Clang-8 C++17 Debug
env: CXX=clang++-8 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
addons: *clang8
- name: Clang-8 C++17 Release
env: CXX=clang++-8 BUILD_TYPE=Release GSL_CXX_STANDARD=17
addons: *clang8
# Clang 9
- name: Clang-9 C++14 Debug
env: CXX=clang++-9 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
addons: &clang9
apt:
sources:
- sourceline: 'deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-9 main'
key_url: https://apt.llvm.org/llvm-snapshot.gpg.key
packages:
- clang-9
- name: Clang-9 C++14 Release
env: CXX=clang++-9 BUILD_TYPE=Release GSL_CXX_STANDARD=14
addons: *clang9
- name: Clang-9 C++17 Debug
env: CXX=clang++-9 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
addons: *clang9
- name: Clang-9 C++17 Release
env: CXX=clang++-9 BUILD_TYPE=Release GSL_CXX_STANDARD=17
addons: *clang9
- env: COMPILER=clang++-7 BUILD_TYPE=Release GSL_CXX_STANDARD=14
addons: *clang70
# Clang 7.0 c++17
- env: COMPILER=clang++-7 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
addons: *clang70
- env: COMPILER=clang++-7 BUILD_TYPE=Release GSL_CXX_STANDARD=17
addons: *clang70
# Clang 10
- name: Clang-10 C++14 Debug
stage: Latest
env: CXX=clang++-10 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
addons: &clang10
apt:
sources:
- sourceline: 'deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-10 main'
key_url: https://apt.llvm.org/llvm-snapshot.gpg.key
packages:
- clang-10
- name: Clang-10 C++14 Release
env: CXX=clang++-10 BUILD_TYPE=Release GSL_CXX_STANDARD=14
addons: *clang10
- name: Clang-10 C++17 Debug
env: CXX=clang++-10 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
addons: *clang10
- name: Clang-10 C++17 Release
env: CXX=clang++-10 BUILD_TYPE=Release GSL_CXX_STANDARD=17
addons: *clang10
##########################################################################
# GCC on Linux
##########################################################################
# GCC 5
- env: COMPILER=g++-5 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
addons: &gcc5
apt:
packages: g++-5
sources:
- ubuntu-toolchain-r-test
- env: COMPILER=g++-5 BUILD_TYPE=Release GSL_CXX_STANDARD=14
addons: *gcc5
# GCC 5 (default on the Xenial image)
- name: GCC-5 C++14 Debug
stage: Legacy
dist: xenial
env: CXX=g++-5 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
- name: GCC-5 C++14 Release
dist: xenial
env: CXX=g++-5 BUILD_TYPE=Release GSL_CXX_STANDARD=14
# GCC 6
- env: COMPILER=g++-6 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
- name: GCC-6 C++14 Debug
env: CXX=g++-6 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
addons: &gcc6
apt:
packages: g++-6
sources:
- ubuntu-toolchain-r-test
- env: COMPILER=g++-6 BUILD_TYPE=Release GSL_CXX_STANDARD=14
- name: GCC-6 C++14 Release
env: CXX=g++-6 BUILD_TYPE=Release GSL_CXX_STANDARD=14
addons: *gcc6
# GCC 7
- env: COMPILER=g++-7 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
addons: &gcc7
# GCC 7 (default on the Bionic image)
- name: GCC-7 C++14 Debug
env: CXX=g++-7 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
- name: GCC-7 C++14 Release
env: CXX=g++-7 BUILD_TYPE=Release GSL_CXX_STANDARD=14
- name: GCC-7 C++17 Debug
env: CXX=g++-7 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
- name: GCC-7 C++17 Release
env: CXX=g++-7 BUILD_TYPE=Release GSL_CXX_STANDARD=17
# GCC 8
- name: GCC-8 C++14 Debug
env: CXX=g++-8 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
addons: &gcc8
apt:
packages: g++-8
- name: GCC-8 C++14 Release
env: CXX=g++-8 BUILD_TYPE=Release GSL_CXX_STANDARD=14
addons: *gcc8
- name: GCC-8 C++17 Debug
env: CXX=g++-8 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
addons: *gcc8
- name: GCC-8 C++17 Release
env: CXX=g++-8 BUILD_TYPE=Release GSL_CXX_STANDARD=17
addons: *gcc8
# GCC 9
- name: GCC-9 C++14 Debug
stage: Latest
env: CXX=g++-9 BUILD_TYPE=Debug GSL_CXX_STANDARD=14
addons: &gcc9
apt:
packages: g++-7
sources:
- ubuntu-toolchain-r-test
- sourceline: ppa:ubuntu-toolchain-r/test
packages:
- g++-9
- name: GCC-9 C++14 Release
env: CXX=g++-9 BUILD_TYPE=Release GSL_CXX_STANDARD=14
addons: *gcc9
- name: GCC-9 C++17 Debug
env: CXX=g++-9 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
addons: *gcc9
- name: GCC-9 C++17 Release
env: CXX=g++-9 BUILD_TYPE=Release GSL_CXX_STANDARD=17
addons: *gcc9
- env: COMPILER=g++-7 BUILD_TYPE=Release GSL_CXX_STANDARD=14
addons: *gcc7
# GCC 7 c++17
- env: COMPILER=g++-7 BUILD_TYPE=Debug GSL_CXX_STANDARD=17
addons: *gcc7
- env: COMPILER=g++-7 BUILD_TYPE=Release GSL_CXX_STANDARD=17
addons: *gcc7
before_install:
- |
# Configuration
JOBS=2 # Travis machines have 2 cores
# Dependencies required by the CI (cached directory)
DEPS_DIR="${TRAVIS_BUILD_DIR}/deps"
- |
# Setup
mkdir -p "${DEPS_DIR:?}" && cd "${DEPS_DIR:?}"
mkdir -p ~/tools && cd ~/tools
if [[ ${TRAVIS_OS_NAME:?} == "osx" ]]; then
export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
fi
- |
# Helper functions
# usage: if [[ $(check_url '<url>') ]]; then ...
function check_url {( set +e
if [[ "$1" =~ 'github.com' ]]; then # check for first byte
if curl --fail --silent --output /dev/null --connect-timeout 12 --range 0-0 "$1"
then echo true; fi
else # request head
if curl --fail --silent --output /dev/null --connect-timeout 12 --head "$1"
then echo true; fi
fi
return
)}
install:
# Set the ${CXX} variable properly
- export CXX=${COMPILER}
- ${CXX} --version
# Dependencies required by the CI are installed in ${TRAVIS_BUILD_DIR}/deps/
- DEPS_DIR="${TRAVIS_BUILD_DIR}/deps"
- mkdir -p "${DEPS_DIR}"
- cd "${DEPS_DIR}"
# Travis machines have 2 cores
- JOBS=2
############################################################################
# Install a recent CMake (unless already installed on OS X)
# Install a different CMake version (or several)
############################################################################
- CMAKE_VERSION=3.7.2
- |
if [[ "${TRAVIS_OS_NAME}" == "linux" ]]; then
CMAKE_URL="https://cmake.org/files/v${CMAKE_VERSION%.[0-9]}/cmake-${CMAKE_VERSION}-Linux-x86_64.tar.gz"
mkdir cmake && travis_retry wget --no-check-certificate -O - ${CMAKE_URL} | tar --strip-components=1 -xz -C cmake
export PATH=${DEPS_DIR}/cmake/bin:${PATH}
else
brew install cmake || brew upgrade cmake
# Install CMake versions
( set -euo pipefail
if [[ ${CMAKE_VERSION:-} ]]; then
if [[ "${TRAVIS_OS_NAME}" == "linux" ]]; then
OS="Linux"; EXT="sh"
if [[ ! ("${CMAKE_VERSION:-}" =~ .+[' '].+) ]]; then
# Single entry -> default CMake version
CMAKE_DEFAULT_DIR="/usr/local"
fi
elif [[ "${TRAVIS_OS_NAME}" == "osx" ]]; then OS="Darwin"; EXT="tar.gz"
else echo "CMake install not supported for this OS."; exit 1
fi
CMAKE_INSTALLER="install-cmake.${EXT}"
fi
for VERSION in ${CMAKE_VERSION:-}; do
CMAKE_URL="https://github.com/Kitware/CMake/releases/download/v${VERSION}/cmake-${VERSION}-${OS}-x86_64.${EXT}"
if [[ $(check_url "$CMAKE_URL") ]]; then
curl -sSL ${CMAKE_URL} -o ${CMAKE_INSTALLER}
CMAKE_DIR="${CMAKE_DEFAULT_DIR:-"${HOME}/tools/cmake-${VERSION}"}"
mkdir -p ${CMAKE_DIR}
if [[ "${TRAVIS_OS_NAME}" == "linux" ]]; then
chmod +x ${CMAKE_INSTALLER}
sudo ./${CMAKE_INSTALLER} --prefix=${CMAKE_DIR} --skip-license
else # OSX
mkdir -p ./CMake_tmp
tar --extract --gzip --file=${CMAKE_INSTALLER} --directory=./CMake_tmp
mv ./CMake_tmp/*/CMake.app/Contents/* ${CMAKE_DIR}
fi
rm --recursive --force ./CMake_tmp ${CMAKE_INSTALLER}
else echo 'Invalid url!'; echo "Version: ${VERSION}"
fi
done
)
if [[ ${CMAKE_VERSION:-} && "${TRAVIS_OS_NAME:?}" == "osx" && ! ("${CMAKE_VERSION:-}" =~ .+[' '].+) ]]
then # Single entry -> default CMake version
export PATH=${HOME}/tools/cmake-${CMAKE_VERSION:?}/bin:$PATH
fi
CMAKE_path=("cmake") # start with installed CMake version
for VERSION in ${CMAKE_VERSION:-}; do
tmp_path="$HOME/tools/cmake-${VERSION:?}/bin/cmake"
if [[ -x "$(command -v ${tmp_path:?})" ]]; then CMAKE_path+=("${tmp_path:?}"); fi
done
function test_CMake_generate {
# $1: cmake or full path to cmake
shopt -s extglob
if [[ "$1" == "cmake" || -x "$(command -v $1)" && "$1" =~ .*cmake$ ]]; then
echo "----------------"
$1 --version
echo "Configuration = ${BUILD_TYPE:-Debug}"
$1 -DCMAKE_BUILD_TYPE=${BUILD_TYPE:-Debug} ${CMAKE_GEN_FLAGS[@]:?} ..
rm -rf !(tests/googletest-*)
if [[ ! ${BUILD_TYPE:-} ]]; then echo "" && echo "Configuration = Release"
$1 -DCMAKE_BUILD_TYPE=Release ${CMAKE_GEN_FLAGS[@]:?} ..
rm -rf !(tests/googletest-*)
fi
else echo "Non existing command: $1"
fi
}
- |
# CMake wrapper (Trusty, Xenial & Bionic); restore default behaviour.
if [[ "${TRAVIS_OS_NAME:?}" == "linux" &&
"$(lsb_release --codename)" =~ (trusty|xenial|bionic)$ ]]
then
if [[ -x $(command -v /usr/local/bin/cmake) ]]; then
function cmake { command /usr/local/bin/cmake $@; }
elif [[ -x $(command -v /usr/bin/cmake) ]]; then
function cmake { command /usr/bin/cmake $@; }
fi
fi
- cmake --version
############################################################################
# [linux]: Install the right version of libc++
# Based on https://github.com/ldionne/hana/blob/master/.travis.yml
############################################################################
- |
LLVM_INSTALL=${DEPS_DIR}/llvm/install
LLVM_INSTALL=${DEPS_DIR:?}/llvm/install
# if in linux and compiler clang and llvm not installed
if [[ "${TRAVIS_OS_NAME}" == "linux" && "${CXX%%+*}" == "clang" && -n "$(ls -A ${LLVM_INSTALL})" ]]; then
if [[ "${TRAVIS_OS_NAME:?}" == "linux" && "${CXX%%+*}" == "clang" && -n "$(ls -A ${LLVM_INSTALL:?})" ]]; then
if [[ "${CXX}" == "clang++-3.6" ]]; then LLVM_VERSION="3.6.2";
elif [[ "${CXX}" == "clang++-3.7" ]]; then LLVM_VERSION="3.7.1";
elif [[ "${CXX}" == "clang++-3.8" ]]; then LLVM_VERSION="3.8.1";
@@ -284,12 +535,17 @@ install:
fi
before_script:
# have CMake to generate build files
- cd "${TRAVIS_BUILD_DIR}"
- mkdir build && cd build
- cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DGSL_CXX_STANDARD=$GSL_CXX_STANDARD
- |
cd "${TRAVIS_BUILD_DIR:?}"
mkdir build && cd build
if [[ ${GSL_CXX_STANDARD:-} ]]; then
CMAKE_GEN_FLAGS=("-DGSL_CXX_STANDARD=$GSL_CXX_STANDARD")
fi
CMAKE_GEN_FLAGS+=("-Wdev -Werror=dev --warn-uninitialized")
script:
# generate build files
- cmake .. -DCMAKE_BUILD_TYPE=${BUILD_TYPE:?} ${CMAKE_GEN_FLAGS[@]:?}
# build and run tests
- cmake --build . -- -j${JOBS}
- ctest --output-on-failure -j${JOBS}
+18 -9
View File
@@ -1,10 +1,13 @@
cmake_minimum_required(VERSION 3.1.3)
cmake_minimum_required(VERSION 3.1.3...3.16)
project(GSL CXX)
include(ExternalProject)
find_package(Git)
# Use GNUInstallDirs to provide the right locations on all platforms
include(GNUInstallDirs)
# creates a library GSL which is an interface (header files only)
add_library(GSL INTERFACE)
@@ -54,15 +57,13 @@ target_compile_definitions(GSL INTERFACE
# the SYSTEM keyword suppresses warnings for users of the library
if(GSL_STANDALONE_PROJECT)
target_include_directories(GSL INTERFACE
$<BUILD_INTERFACE:
${CMAKE_CURRENT_SOURCE_DIR}/include
>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
else()
target_include_directories(GSL SYSTEM INTERFACE
$<BUILD_INTERFACE:
${CMAKE_CURRENT_SOURCE_DIR}/include
>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
endif()
@@ -77,15 +78,23 @@ if (CMAKE_VERSION VERSION_GREATER 3.7.8)
# add natvis file to the library so it will automatically be loaded into Visual Studio
if(VS_ADD_NATIVE_VISUALIZERS)
target_sources(GSL INTERFACE
${CMAKE_CURRENT_SOURCE_DIR}/GSL.natvis
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/GSL.natvis>
)
endif()
endif()
install(TARGETS GSL EXPORT Microsoft.GSLConfig)
install(
DIRECTORY include/gsl
DESTINATION include
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
# Make library importable by other projects
install(EXPORT Microsoft.GSLConfig NAMESPACE Microsoft.GSL:: DESTINATION share/Microsoft.GSL/cmake)
export(TARGETS GSL NAMESPACE Microsoft.GSL:: FILE Microsoft.GSLConfig.cmake)
# Add Microsoft.GSL::GSL alias for GSL so that dependents can be agnostic about
# whether GSL was added via `add_subdirectory` or `find_package`
add_library(Microsoft.GSL::GSL ALIAS GSL)
option(GSL_TEST "Generate tests." ${GSL_STANDALONE_PROJECT})
if (GSL_TEST)
+23 -2
View File
@@ -18,7 +18,7 @@ other platforms. Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for more inform
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
# Usage of Third Party Libraries
This project makes use of the [Catch](https://github.com/philsquared/catch) testing library. Please see the [ThirdPartyNotices.txt](./ThirdPartyNotices.txt) file for details regarding the licensing of Catch.
This project makes use of the [Google Test](https://github.com/google/googletest) testing library. Please see the [ThirdPartyNotices.txt](./ThirdPartyNotices.txt) file for details regarding the licensing of Google Test.
# Quick Start
## Supported Platforms
@@ -53,7 +53,7 @@ contributing any changes that were necessary back to this project to benefit the
## Building the tests
To build the tests, you will require the following:
* [CMake](http://cmake.org), version 3.1.3 or later to be installed and in your PATH.
* [CMake](http://cmake.org), version 3.1.3 (3.2.3 for AppleClang) or later to be installed and in your PATH.
These steps assume the source code of this repository has been cloned into a directory named `c:\GSL`.
@@ -77,6 +77,18 @@ These steps assume the source code of this repository has been cloned into a dir
All tests should pass - indicating your platform is fully supported and you are ready to use the GSL types!
## Building GSL - Using vcpkg
You can download and install GSL using the [vcpkg](https://github.com/Microsoft/vcpkg) dependency manager:
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
vcpkg install ms-gsl
The GSL port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please [create an issue or pull request](https://github.com/Microsoft/vcpkg) on the vcpkg repository.
## Using the libraries
As the types are entirely implemented inline in headers, there are no linking requirements.
@@ -97,5 +109,14 @@ Include the library using:
#include <gsl/gsl>
## Usage in CMake
The library provides a Config file for CMake, once installed it can be found via
find_package(Microsoft.GSL CONFIG)
Which, when successful, will add library target called `Microsoft.GSL::GSL` which you can use via the usual
`target_link_libraries` mechanism.
## Debugging visualization support
For Visual Studio users, the file [GSL.natvis](./GSL.natvis) in the root directory of the repository can be added to your project if you would like more helpful visualization of GSL types in the Visual Studio debugger than would be offered by default.
+33 -31
View File
@@ -2,38 +2,40 @@
THIRD-PARTY SOFTWARE NOTICES AND INFORMATION
Do Not Translate or Localize
GSL: Guidelines Support Library incorporates third party material from the projects listed below. The original copyright notice and the license under which Microsoft received such third party material are set forth below. Microsoft reserves all other rights not expressly granted, whether by implication, estoppel or otherwise.
GSL: Guidelines Support Library incorporates third party material from the projects listed below.
-------------------------------------------------------------------------------
Software: Google Test
Owner: Google Inc.
Source URL: github.com/google/googletest
License: BSD 3 - Clause
Text:
Copyright 2008, Google Inc.
All rights reserved.
1. Catch (https://github.com/philsquared/Catch)
%% Catch NOTICES, INFORMATION, AND LICENSE BEGIN HERE
=========================================
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
=========================================
END OF Catch NOTICES, INFORMATION, AND LICENSE
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-------------------------------------------------------------------------------
+2 -2
View File
@@ -37,8 +37,8 @@ namespace gsl
{
// Note: this will generate faster code than std::copy using span iterator in older msvc+stl
// not necessary for msvc since VS2017 15.8 (_MSC_VER >= 1915)
template <class SrcElementType, std::ptrdiff_t SrcExtent, class DestElementType,
std::ptrdiff_t DestExtent>
template <class SrcElementType, std::size_t SrcExtent, class DestElementType,
std::size_t DestExtent>
void copy(span<SrcElementType, SrcExtent> src, span<DestElementType, DestExtent> dest)
{
static_assert(std::is_assignable<decltype(*dest.data()), decltype(*src.data())>::value,
+27 -71
View File
@@ -17,8 +17,26 @@
#ifndef GSL_CONTRACTS_H
#define GSL_CONTRACTS_H
//
// Temporary until MSVC STL supports no-exceptions mode.
// Currently terminate is a no-op in this mode, so we add termination behavior back
//
#if defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS))
#define GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND
#include <intrin.h>
#define RANGE_CHECKS_FAILURE 0
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Winvalid-noreturn"
#endif // defined(__clang__)
#else // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS))
#include <exception>
#include <stdexcept> // for logic_error
#endif // defined(_MSC_VER) && (defined(_KERNEL_MODE) || (defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS))
//
// make suppress attributes parse for some compilers
@@ -34,45 +52,18 @@
#endif // _MSC_VER
#endif // __clang__
//
// Temporary until MSVC STL supports no-exceptions mode.
// Currently terminate is a no-op in this mode, so we add termination behavior back
//
#if defined(_MSC_VER) && defined(_HAS_EXCEPTIONS) && !_HAS_EXCEPTIONS
#define GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND
#include <intrin.h>
#define RANGE_CHECKS_FAILURE 0
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Winvalid-noreturn"
#endif
#endif
//
// There are three configuration options for this GSL implementation's behavior
// when pre/post conditions on the GSL types are violated:
//
// 1. GSL_TERMINATE_ON_CONTRACT_VIOLATION: std::terminate will be called (default)
// 2. GSL_THROW_ON_CONTRACT_VIOLATION: a gsl::fail_fast exception will be thrown
// 3. GSL_UNENFORCED_ON_CONTRACT_VIOLATION: nothing happens
//
#if !(defined(GSL_THROW_ON_CONTRACT_VIOLATION) || defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION) || \
defined(GSL_UNENFORCED_ON_CONTRACT_VIOLATION))
#define GSL_TERMINATE_ON_CONTRACT_VIOLATION
#endif
#define GSL_STRINGIFY_DETAIL(x) #x
#define GSL_STRINGIFY(x) GSL_STRINGIFY_DETAIL(x)
#if defined(__clang__) || defined(__GNUC__)
#define GSL_LIKELY(x) __builtin_expect(!!(x), 1)
#define GSL_UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
#define GSL_LIKELY(x) (!!(x))
#define GSL_UNLIKELY(x) (!!(x))
#endif
#endif // defined(__clang__) || defined(__GNUC__)
//
// GSL_ASSUME(cond)
@@ -94,18 +85,16 @@
namespace gsl
{
struct fail_fast : public std::logic_error
{
explicit fail_fast(char const* const message) : std::logic_error(message) {}
};
namespace details
{
#if defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
typedef void (__cdecl *terminate_handler)();
typedef void(__cdecl* terminate_handler)();
// clang-format off
GSL_SUPPRESS(f.6) // NO-FORMAT: attribute
// clang-format on
[[noreturn]] inline void __cdecl default_terminate_handler()
{
__fastfail(RANGE_CHECKS_FAILURE);
@@ -117,7 +106,7 @@ namespace details
return handler;
}
#endif
#endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
[[noreturn]] inline void terminate() noexcept
{
@@ -125,48 +114,15 @@ namespace details
(*gsl::details::get_terminate_handler())();
#else
std::terminate();
#endif
#endif // defined(GSL_MSVC_USE_STL_NOEXCEPTION_WORKAROUND)
}
#if defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION)
template <typename Exception>
[[noreturn]] void throw_exception(Exception&&) noexcept
{
gsl::details::terminate();
}
#else
template <typename Exception>
[[noreturn]] void throw_exception(Exception&& exception)
{
throw std::forward<Exception>(exception);
}
#endif // GSL_TERMINATE_ON_CONTRACT_VIOLATION
} // namespace details
} // namespace gsl
#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
#define GSL_CONTRACT_CHECK(type, cond) \
(GSL_LIKELY(cond) ? static_cast<void>(0) \
: gsl::details::throw_exception(gsl::fail_fast( \
"GSL: " type " failure at " __FILE__ ": " GSL_STRINGIFY(__LINE__))))
#elif defined(GSL_TERMINATE_ON_CONTRACT_VIOLATION)
#define GSL_CONTRACT_CHECK(type, cond) \
(GSL_LIKELY(cond) ? static_cast<void>(0) : gsl::details::terminate())
#elif defined(GSL_UNENFORCED_ON_CONTRACT_VIOLATION)
#define GSL_CONTRACT_CHECK(type, cond) GSL_ASSUME(cond)
#endif // GSL_THROW_ON_CONTRACT_VIOLATION
#define Expects(cond) GSL_CONTRACT_CHECK("Precondition", cond)
#define Ensures(cond) GSL_CONTRACT_CHECK("Postcondition", cond)
+4
View File
@@ -87,6 +87,10 @@
#define byte_may_alias
#endif // defined __clang__ || defined __GNUC__
#if GSL_USE_STD_BYTE
#include <cstddef>
#endif
namespace gsl
{
#if GSL_USE_STD_BYTE
+2 -7
View File
@@ -21,7 +21,6 @@
#include <array>
#include <cstddef> // for ptrdiff_t, size_t
#include <exception> // for exception
#include <initializer_list> // for initializer_list
#include <type_traits> // for is_signed, integral_constant
#include <utility> // for exchange, forward
@@ -97,10 +96,6 @@ constexpr T narrow_cast(U&& u) noexcept
return static_cast<T>(std::forward<U>(u));
}
struct narrowing_error : public std::exception
{
};
namespace details
{
template <class T, class U>
@@ -120,9 +115,9 @@ constexpr
T narrow(U u) noexcept(false)
{
T t = narrow_cast<T>(u);
if (static_cast<U>(t) != u) gsl::details::throw_exception(narrowing_error());
if (static_cast<U>(t) != u) gsl::details::terminate();
if (!details::is_same_signedness<T, U>::value && ((t < T{}) != (u < U{})))
gsl::details::throw_exception(narrowing_error());
gsl::details::terminate();
return t;
}
+2 -2
View File
@@ -57,7 +57,7 @@
#endif // _MSC_VER < 1910
#endif // _MSC_VER
#if __clang__ || __GNUC__
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
@@ -2266,7 +2266,7 @@ general_span_iterator<Span> operator+(typename general_span_iterator<Span>::diff
#pragma GCC diagnostic pop
#endif // __GNUC__ > 6
#if __clang__ || __GNUC__
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif
-1
View File
@@ -88,7 +88,6 @@ public:
{
}
not_null(not_null&& other) = default;
not_null(const not_null& other) = default;
not_null& operator=(const not_null& other) = default;
+300 -334
View File
@@ -21,21 +21,18 @@
#include <gsl/gsl_byte> // for byte
#include <gsl/gsl_util> // for narrow_cast, narrow
#include <algorithm> // for lexicographical_compare
#include <array> // for array
#include <cstddef> // for ptrdiff_t, size_t, nullptr_t
#include <iterator> // for reverse_iterator, distance, random_access_...
#include <limits>
#include <stdexcept>
#include <array> // for array
#include <cstddef> // for ptrdiff_t, size_t, nullptr_t
#include <iterator> // for reverse_iterator, distance, random_access_...
#include <type_traits> // for enable_if_t, declval, is_convertible, inte...
#include <utility>
#include <memory> // for std::addressof
#if defined(_MSC_VER) && !defined(__clang__)
#pragma warning(push)
// turn off some warnings that are noisy about our Expects statements
#pragma warning(disable : 4127) // conditional expression is constant
#pragma warning( \
disable : 4146) // unary minus operator applied to unsigned type, result still unsigned
#pragma warning(disable : 4702) // unreachable code
// Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool.
@@ -58,7 +55,7 @@
// GCC 7 does not like the signed unsigned missmatch (size_t ptrdiff_t)
// While there is a conversion from signed to unsigned, it happens at
// compiletime, so the compiler wouldn't have to warn indiscriminently, but
// compiletime, so the compiler wouldn't have to warn indiscriminately, but
// could check if the source value actually doesn't fit into the target type
// and only warn in those cases.
#if defined(__GNUC__) && __GNUC__ > 6
@@ -70,9 +67,9 @@ namespace gsl
{
// [views.constants], constants
constexpr const std::ptrdiff_t dynamic_extent = -1;
constexpr const std::size_t dynamic_extent = static_cast<std::size_t>(-1);
template <class ElementType, std::ptrdiff_t Extent = dynamic_extent>
template <class ElementType, std::size_t Extent = dynamic_extent>
class span;
// implementation details
@@ -83,7 +80,7 @@ namespace details
{
};
template <class ElementType, std::ptrdiff_t Extent>
template <class ElementType, std::size_t Extent>
struct is_span_oracle<gsl::span<ElementType, Extent>> : std::true_type
{
};
@@ -104,156 +101,189 @@ namespace details
};
template <class T>
struct is_std_array : public is_std_array_oracle<std::remove_cv_t<T>>
struct is_std_array : is_std_array_oracle<std::remove_cv_t<T>>
{
};
template <std::ptrdiff_t From, std::ptrdiff_t To>
template <std::size_t From, std::size_t To>
struct is_allowed_extent_conversion
: public std::integral_constant<bool, From == To || From == gsl::dynamic_extent ||
To == gsl::dynamic_extent>
: std::integral_constant<bool, From == To || To == gsl::dynamic_extent>
{
};
template <class From, class To>
struct is_allowed_element_type_conversion
: public std::integral_constant<bool, std::is_convertible<From (*)[], To (*)[]>::value>
: std::integral_constant<bool, std::is_convertible<From (*)[], To (*)[]>::value>
{
};
template <class Span, bool IsConst>
template <class Type>
class span_iterator
{
using element_type_ = typename Span::element_type;
public:
#ifdef _MSC_VER
// Tell Microsoft standard library that span_iterators are checked.
using _Unchecked_type = typename Span::pointer;
#endif
using iterator_category = std::random_access_iterator_tag;
using value_type = std::remove_cv_t<element_type_>;
using difference_type = typename Span::index_type;
using value_type = std::remove_cv_t<Type>;
using difference_type = std::ptrdiff_t;
using pointer = Type*;
using reference = Type&;
using reference = std::conditional_t<IsConst, const element_type_, element_type_>&;
using pointer = std::add_pointer_t<reference>;
#ifdef _MSC_VER
using _Unchecked_type = pointer;
#endif // _MSC_VER
constexpr span_iterator() = default;
span_iterator() = default;
constexpr span_iterator(const Span* span, difference_type idx) noexcept
: span_(span), index_(idx)
constexpr span_iterator(pointer begin, pointer end, pointer current)
: begin_(begin), end_(end), current_(current)
{}
friend span_iterator<Span, true>;
template <bool B, std::enable_if_t<!B && IsConst>* = nullptr>
constexpr span_iterator(const span_iterator<Span, B>& other) noexcept
: span_iterator(other.span_, other.index_)
{}
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
constexpr reference operator*() const
constexpr operator span_iterator<const Type>() const noexcept
{
Expects(index_ != span_->size());
return *(span_->data() + index_);
return {begin_, end_, current_};
}
constexpr pointer operator->() const
constexpr reference operator*() const noexcept
{
Expects(index_ != span_->size());
return span_->data() + index_;
Expects(begin_ && end_);
Expects(begin_ <= current_ && current_ < end_);
return *current_;
}
constexpr span_iterator& operator++()
constexpr pointer operator->() const noexcept
{
Expects(0 <= index_ && index_ != span_->size());
++index_;
Expects(begin_ && end_);
Expects(begin_ <= current_ && current_ < end_);
return current_;
}
constexpr span_iterator& operator++() noexcept
{
Expects(begin_ && current_ && end_);
Expects(current_ < end_);
++current_;
return *this;
}
constexpr span_iterator operator++(int)
constexpr span_iterator operator++(int) noexcept
{
auto ret = *this;
++(*this);
span_iterator ret = *this;
++*this;
return ret;
}
constexpr span_iterator& operator--()
constexpr span_iterator& operator--() noexcept
{
Expects(index_ != 0 && index_ <= span_->size());
--index_;
Expects(begin_ && end_);
Expects(begin_ < current_);
--current_;
return *this;
}
constexpr span_iterator operator--(int)
constexpr span_iterator operator--(int) noexcept
{
auto ret = *this;
--(*this);
span_iterator ret = *this;
--*this;
return ret;
}
constexpr span_iterator operator+(difference_type n) const
constexpr span_iterator& operator+=(const difference_type n) noexcept
{
auto ret = *this;
return ret += n;
if (n != 0) Expects(begin_ && current_ && end_);
if (n > 0) Expects(end_ - current_ >= n);
if (n < 0) Expects(current_ - begin_ >= -n);
current_ += n;
return *this;
}
friend constexpr span_iterator operator+(difference_type n, span_iterator const& rhs)
constexpr span_iterator operator+(const difference_type n) const noexcept
{
span_iterator ret = *this;
ret += n;
return ret;
}
friend constexpr span_iterator operator+(const difference_type n,
const span_iterator& rhs) noexcept
{
return rhs + n;
}
constexpr span_iterator& operator+=(difference_type n)
constexpr span_iterator& operator-=(const difference_type n) noexcept
{
Expects((index_ + n) >= 0 && (index_ + n) <= span_->size());
index_ += n;
if (n != 0) Expects(begin_ && current_ && end_);
if (n > 0) Expects(current_ - begin_ >= n);
if (n < 0) Expects(end_ - current_ >= -n);
current_ -= n;
return *this;
}
constexpr span_iterator operator-(difference_type n) const
constexpr span_iterator operator-(const difference_type n) const noexcept
{
auto ret = *this;
return ret -= n;
span_iterator ret = *this;
ret -= n;
return ret;
}
constexpr span_iterator& operator-=(difference_type n) { return *this += -n; }
constexpr difference_type operator-(span_iterator rhs) const
template <
class Type2,
std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
constexpr difference_type operator-(const span_iterator<Type2>& rhs) const noexcept
{
Expects(span_ == rhs.span_);
return index_ - rhs.index_;
Expects(begin_ == rhs.begin_ && end_ == rhs.end_);
return current_ - rhs.current_;
}
constexpr reference operator[](difference_type n) const { return *(*this + n); }
constexpr friend bool operator==(span_iterator lhs, span_iterator rhs) noexcept
constexpr reference operator[](const difference_type n) const noexcept
{
return lhs.span_ == rhs.span_ && lhs.index_ == rhs.index_;
return *(*this + n);
}
constexpr friend bool operator!=(span_iterator lhs, span_iterator rhs) noexcept
template <
class Type2,
std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
constexpr bool operator==(const span_iterator<Type2>& rhs) const noexcept
{
return !(lhs == rhs);
Expects(begin_ == rhs.begin_ && end_ == rhs.end_);
return current_ == rhs.current_;
}
constexpr friend bool operator<(span_iterator lhs, span_iterator rhs) noexcept
template <
class Type2,
std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
constexpr bool operator!=(const span_iterator<Type2>& rhs) const noexcept
{
return lhs.index_ < rhs.index_;
return !(*this == rhs);
}
constexpr friend bool operator<=(span_iterator lhs, span_iterator rhs) noexcept
template <
class Type2,
std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
constexpr bool operator<(const span_iterator<Type2>& rhs) const noexcept
{
return !(rhs < lhs);
Expects(begin_ == rhs.begin_ && end_ == rhs.end_);
return current_ < rhs.current_;
}
constexpr friend bool operator>(span_iterator lhs, span_iterator rhs) noexcept
template <
class Type2,
std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
constexpr bool operator>(const span_iterator<Type2>& rhs) const noexcept
{
return rhs < lhs;
return rhs < *this;
}
constexpr friend bool operator>=(span_iterator lhs, span_iterator rhs) noexcept
template <
class Type2,
std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
constexpr bool operator<=(const span_iterator<Type2>& rhs) const noexcept
{
return !(rhs > lhs);
return !(rhs < *this);
}
template <
class Type2,
std::enable_if_t<std::is_same<std::remove_cv_t<Type2>, value_type>::value, int> = 0>
constexpr bool operator>=(const span_iterator<Type2>& rhs) const noexcept
{
return !(*this < rhs);
}
#ifdef _MSC_VER
@@ -262,22 +292,26 @@ namespace details
// algorithm calls
friend constexpr void _Verify_range(span_iterator lhs, span_iterator rhs) noexcept
{ // test that [lhs, rhs) forms a valid range inside an STL algorithm
Expects(lhs.span_ == rhs.span_ // range spans have to match
&& lhs.index_ <= rhs.index_); // range must not be transposed
Expects(lhs.begin_ == rhs.begin_ // range spans have to match
&& lhs.end_ == rhs.end_ &&
lhs.current_ <= rhs.current_); // range must not be transposed
}
constexpr void _Verify_offset(const difference_type n) const noexcept
{ // test that the iterator *this + n is a valid range in an STL
// algorithm call
Expects((index_ + n) >= 0 && (index_ + n) <= span_->size());
{ // test that *this + n is within the range of this call
if (n != 0) Expects(begin_ && current_ && end_);
if (n > 0) Expects(end_ - current_ >= n);
if (n < 0) Expects(current_ - begin_ >= -n);
}
// clang-format off
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
// clang-format on
constexpr pointer _Unwrapped() const noexcept
{ // after seeking *this to a high water mark, or using one of the
// _Verify_xxx functions above, unwrap this span_iterator to a raw
// pointer
return span_->data() + index_;
return current_;
}
// Tell the STL that span_iterator should not be unwrapped if it can't
@@ -287,61 +321,64 @@ namespace details
#else
static constexpr bool _Unwrap_when_unverified = false;
#endif
// clang-format off
GSL_SUPPRESS(con.3) // NO-FORMAT: attribute // TODO: false positive
// clang-format on
constexpr void _Seek_to(const pointer p) noexcept
{ // adjust the position of *this to previously verified location p
// after _Unwrapped
index_ = p - span_->data();
current_ = p;
}
#endif
protected:
const Span* span_ = nullptr;
std::ptrdiff_t index_ = 0;
pointer begin_ = nullptr;
pointer end_ = nullptr;
pointer current_ = nullptr;
};
template <std::ptrdiff_t Ext>
template <std::size_t Ext>
class extent_type
{
public:
using index_type = std::ptrdiff_t;
using size_type = std::size_t;
static_assert(Ext >= 0, "A fixed-size span must be >= 0 in size.");
constexpr extent_type() noexcept = default;
constexpr extent_type() noexcept {}
template <index_type Other>
template <size_type Other>
constexpr extent_type(extent_type<Other> ext)
{
static_assert(Other == Ext || Other == dynamic_extent,
static_assert(Other == Ext,
"Mismatch between fixed-size extent and size of initializing data.");
Expects(ext.size() == Ext);
}
constexpr extent_type(index_type size) { Expects(size == Ext); }
constexpr extent_type(size_type size) { Expects(size == Ext); }
constexpr index_type size() const noexcept { return Ext; }
constexpr size_type size() const noexcept { return Ext; }
};
template <>
class extent_type<dynamic_extent>
{
public:
using index_type = std::ptrdiff_t;
using size_type = std::size_t;
template <index_type Other>
template <size_type Other>
explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size())
{}
explicit constexpr extent_type(index_type size) : size_(size) { Expects(size >= 0); }
explicit constexpr extent_type(size_type size) : size_(size)
{
Expects(size != dynamic_extent);
}
constexpr index_type size() const noexcept { return size_; }
constexpr size_type size() const noexcept { return size_; }
private:
index_type size_;
size_type size_;
};
template <class ElementType, std::ptrdiff_t Extent, std::ptrdiff_t Offset, std::ptrdiff_t Count>
template <class ElementType, std::size_t Extent, std::size_t Offset, std::size_t Count>
struct calculate_subspan_type
{
using type = span<ElementType, Count != dynamic_extent
@@ -351,99 +388,106 @@ namespace details
} // namespace details
// [span], class template span
template <class ElementType, std::ptrdiff_t Extent>
template <class ElementType, std::size_t Extent>
class span
{
public:
// constants and types
using element_type = ElementType;
using value_type = std::remove_cv_t<ElementType>;
using index_type = std::ptrdiff_t;
using size_type = std::size_t;
using pointer = element_type*;
using const_pointer = const element_type*;
using reference = element_type&;
using const_reference = const element_type&;
using difference_type = std::ptrdiff_t;
using iterator = details::span_iterator<span<ElementType, Extent>, false>;
using const_iterator = details::span_iterator<span<ElementType, Extent>, true>;
using iterator = details::span_iterator<ElementType>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using size_type = index_type;
#if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
static constexpr const index_type extent{Extent};
static constexpr const size_type extent{Extent};
#else
static constexpr index_type extent{Extent};
static constexpr size_type extent{Extent};
#endif
// [span.cons], span constructors, copy, assignment, and destructor
template <bool Dependent = false,
// "Dependent" is needed to make "std::enable_if_t<Dependent || Extent <= 0>" SFINAE,
// since "std::enable_if_t<Extent <= 0>" is ill-formed when Extent is greater than 0.
class = std::enable_if_t<(Dependent || Extent <= 0)>>
// "Dependent" is needed to make "std::enable_if_t<Dependent || Extent == 0 || Extent
// == dynamic_extent>" SFINAE, since "std::enable_if_t<Extent == 0 || Extent ==
// dynamic_extent>" is ill-formed when Extent is greater than 0.
class = std::enable_if_t<(Dependent ||
details::is_allowed_extent_conversion<0, Extent>::value)>>
constexpr span() noexcept : storage_(nullptr, details::extent_type<0>())
{}
constexpr span(pointer ptr, index_type count) : storage_(ptr, count) {}
constexpr span(pointer ptr, size_type count) noexcept : storage_(ptr, count)
{
if (Extent != dynamic_extent) Expects(count == Extent);
}
constexpr span(pointer firstElem, pointer lastElem)
: storage_(firstElem, std::distance(firstElem, lastElem))
{}
constexpr span(pointer firstElem, pointer lastElem) noexcept
: storage_(firstElem, static_cast<std::size_t>(lastElem - firstElem))
{
if (Extent != dynamic_extent)
{ Expects(lastElem - firstElem == static_cast<difference_type>(Extent)); }
}
template <std::size_t N>
template <std::size_t N,
std::enable_if_t<details::is_allowed_extent_conversion<N, Extent>::value, int> = 0>
constexpr span(element_type (&arr)[N]) noexcept
: storage_(KnownNotNull{std::addressof(arr[0])}, details::extent_type<N>())
: storage_(KnownNotNull{arr + 0}, details::extent_type<N>())
{}
template <std::size_t N, class = std::enable_if_t<(N > 0)>>
constexpr span(std::array<std::remove_const_t<element_type>, N>& arr) noexcept
template <
class T, std::size_t N,
std::enable_if_t<(details::is_allowed_extent_conversion<N, Extent>::value &&
details::is_allowed_element_type_conversion<T, element_type>::value),
int> = 0>
constexpr span(std::array<T, N>& arr) noexcept
: storage_(KnownNotNull{arr.data()}, details::extent_type<N>())
{
}
{}
constexpr span(std::array<std::remove_const_t<element_type>, 0>&) noexcept
: storage_(static_cast<pointer>(nullptr), details::extent_type<0>())
{
}
template <std::size_t N, class = std::enable_if_t<(N > 0)>>
constexpr span(const std::array<std::remove_const_t<element_type>, N>& arr) noexcept
template <class T, std::size_t N,
std::enable_if_t<
(details::is_allowed_extent_conversion<N, Extent>::value &&
details::is_allowed_element_type_conversion<const T, element_type>::value),
int> = 0>
constexpr span(const std::array<T, N>& arr) noexcept
: storage_(KnownNotNull{arr.data()}, details::extent_type<N>())
{
}
{}
constexpr span(const std::array<std::remove_const_t<element_type>, 0>&) noexcept
: storage_(static_cast<pointer>(nullptr), details::extent_type<0>())
{
}
// NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the requirement
// NB: the SFINAE here uses .data() as an incomplete/imperfect proxy for the requirement
// on Container to be a contiguous sequence container.
template <class Container,
class = std::enable_if_t<
!details::is_span<Container>::value && !details::is_std_array<Container>::value &&
std::is_convertible<typename Container::pointer, pointer>::value &&
std::is_convertible<typename Container::pointer,
decltype(std::declval<Container>().data())>::value>>
constexpr span(Container& cont) : span(cont.data(), narrow<index_type>(cont.size()))
std::is_pointer<decltype(std::declval<Container&>().data())>::value &&
std::is_convertible<
std::remove_pointer_t<decltype(std::declval<Container&>().data())> (*)[],
element_type (*)[]>::value>>
constexpr span(Container& cont) noexcept : span(cont.data(), cont.size())
{}
template <class Container,
class = std::enable_if_t<
std::is_const<element_type>::value && !details::is_span<Container>::value &&
std::is_convertible<typename Container::pointer, pointer>::value &&
std::is_convertible<typename Container::pointer,
decltype(std::declval<Container>().data())>::value>>
constexpr span(const Container& cont) : span(cont.data(), narrow<index_type>(cont.size()))
!details::is_std_array<Container>::value &&
std::is_pointer<decltype(std::declval<const Container&>().data())>::value &&
std::is_convertible<std::remove_pointer_t<
decltype(std::declval<const Container&>().data())> (*)[],
element_type (*)[]>::value>>
constexpr span(const Container& cont) noexcept : span(cont.data(), cont.size())
{}
constexpr span(const span& other) noexcept = default;
template <
class OtherElementType, std::ptrdiff_t OtherExtent,
class OtherElementType, std::size_t OtherExtent,
class = std::enable_if_t<
details::is_allowed_extent_conversion<OtherExtent, Extent>::value &&
details::is_allowed_element_type_conversion<OtherElementType, element_type>::value>>
constexpr span(const span<OtherElementType, OtherExtent>& other)
constexpr span(const span<OtherElementType, OtherExtent>& other) noexcept
: storage_(other.data(), details::extent_type<OtherExtent>(other.size()))
{}
@@ -451,125 +495,119 @@ public:
constexpr span& operator=(const span& other) noexcept = default;
// [span.sub], span subviews
template <std::ptrdiff_t Count>
constexpr span<element_type, Count> first() const
template <std::size_t Count>
constexpr span<element_type, Count> first() const noexcept
{
Expects(Count >= 0 && Count <= size());
Expects(Count <= size());
return {data(), Count};
}
template <std::ptrdiff_t Count>
template <std::size_t Count>
// clang-format off
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
constexpr span<element_type, Count> last() const
// clang-format on
constexpr span<element_type, Count> last() const noexcept
{
Expects(Count >= 0 && size() - Count >= 0);
Expects(Count <= size());
return {data() + (size() - Count), Count};
}
template <std::ptrdiff_t Offset, std::ptrdiff_t Count = dynamic_extent>
template <std::size_t Offset, std::size_t Count = dynamic_extent>
// clang-format off
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
constexpr auto subspan() const ->
// clang-format on
constexpr auto subspan() const noexcept ->
typename details::calculate_subspan_type<ElementType, Extent, Offset, Count>::type
{
Expects((Offset >= 0 && size() - Offset >= 0) &&
(Count == dynamic_extent || (Count >= 0 && Offset + Count <= size())));
Expects((size() >= Offset) && (Count == dynamic_extent || (Count <= size() - Offset)));
return {data() + Offset, Count == dynamic_extent ? size() - Offset : Count};
}
constexpr span<element_type, dynamic_extent> first(index_type count) const
constexpr span<element_type, dynamic_extent> first(size_type count) const noexcept
{
Expects(count >= 0 && count <= size());
Expects(count <= size());
return {data(), count};
}
constexpr span<element_type, dynamic_extent> last(index_type count) const
constexpr span<element_type, dynamic_extent> last(size_type count) const noexcept
{
Expects(count <= size());
return make_subspan(size() - count, dynamic_extent, subspan_selector<Extent>{});
}
constexpr span<element_type, dynamic_extent> subspan(index_type offset,
index_type count = dynamic_extent) const
constexpr span<element_type, dynamic_extent> subspan(size_type offset,
size_type count = dynamic_extent) const
noexcept
{
return make_subspan(offset, count, subspan_selector<Extent>{});
}
// [span.obs], span observers
constexpr index_type size() const noexcept { return storage_.size(); }
constexpr index_type size_bytes() const noexcept
constexpr size_type size() const noexcept { return storage_.size(); }
constexpr size_type size_bytes() const noexcept
{
return size() * narrow_cast<index_type>(sizeof(element_type));
Expects(size() < dynamic_extent / sizeof(element_type));
return size() * sizeof(element_type);
}
constexpr bool empty() const noexcept { return size() == 0; }
// [span.elem], span element access
// clang-format off
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
constexpr reference operator[](index_type idx) const
// clang-format on
constexpr reference operator[](size_type idx) const noexcept
{
Expects(CheckRange(idx, storage_.size()));
Expects(idx < size());
return data()[idx];
}
constexpr reference at(index_type idx) const { return this->operator[](idx); }
constexpr reference operator()(index_type idx) const { return this->operator[](idx); }
constexpr reference front() const noexcept
{
Expects(size() > 0);
return data()[0];
}
constexpr reference back() const noexcept
{
Expects(size() > 0);
return data()[size() - 1];
}
constexpr pointer data() const noexcept { return storage_.data(); }
// [span.iter], span iterator support
constexpr iterator begin() const noexcept { return {this, 0}; }
constexpr iterator end() const noexcept { return {this, size()}; }
constexpr iterator begin() const noexcept
{
const auto data = storage_.data();
return {data, data + size(), data};
}
constexpr const_iterator cbegin() const noexcept { return {this, 0}; }
constexpr const_iterator cend() const noexcept { return {this, size()}; }
constexpr iterator end() const noexcept
{
const auto data = storage_.data();
const auto endData = data + storage_.size();
return {data, endData, endData};
}
constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; }
constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; }
constexpr const_reverse_iterator crbegin() const noexcept
{
return const_reverse_iterator{cend()};
}
constexpr const_reverse_iterator crend() const noexcept
{
return const_reverse_iterator{cbegin()};
}
#ifdef _MSC_VER
// Tell MSVC how to unwrap spans in range-based-for
constexpr pointer _Unchecked_begin() const noexcept { return data(); }
constexpr pointer _Unchecked_end() const noexcept
{
// clang-format off
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
// clang-format on
return data() + size();
}
#endif // _MSC_VER
private:
static constexpr bool CheckRange(index_type idx, index_type size) noexcept
{
// Optimization:
//
// idx >= 0 && idx < size
// =>
// static_cast<size_t>(idx) < static_cast<size_t>(size)
//
// because size >=0 by span construction, and negative idx will
// wrap around to a value always greater than size when casted.
// check if we have enough space to wrap around
#if defined(__cpp_if_constexpr)
if constexpr (sizeof(index_type) <= sizeof(size_t))
#else
if (sizeof(index_type) <= sizeof(size_t))
#endif
{
return narrow_cast<size_t>(idx) < narrow_cast<size_t>(size);
}
else
{
return idx >= 0 && idx < size;
}
}
// Needed to remove unnecessary null check in subspans
struct KnownNotNull
{
@@ -589,13 +627,13 @@ private:
constexpr storage_type(KnownNotNull data, OtherExtentType ext)
: ExtentType(ext), data_(data.p)
{
Expects(ExtentType::size() >= 0);
Expects(ExtentType::size() != dynamic_extent);
}
template <class OtherExtentType>
constexpr storage_type(pointer data, OtherExtentType ext) : ExtentType(ext), data_(data)
{
Expects(ExtentType::size() >= 0);
Expects(ExtentType::size() != dynamic_extent);
Expects(data || ExtentType::size() == 0);
}
@@ -609,76 +647,56 @@ private:
// The rest is needed to remove unnecessary null check
// in subspans and constructors from arrays
constexpr span(KnownNotNull ptr, index_type count) : storage_(ptr, count) {}
constexpr span(KnownNotNull ptr, size_type count) noexcept : storage_(ptr, count) {}
template <std::ptrdiff_t CallerExtent>
template <std::size_t CallerExtent>
class subspan_selector
{
};
template <std::ptrdiff_t CallerExtent>
span<element_type, dynamic_extent> make_subspan(index_type offset, index_type count,
subspan_selector<CallerExtent>) const
template <std::size_t CallerExtent>
constexpr span<element_type, dynamic_extent> make_subspan(size_type offset, size_type count,
subspan_selector<CallerExtent>) const
noexcept
{
const span<element_type, dynamic_extent> tmp(*this);
return tmp.subspan(offset, count);
}
// clang-format off
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
span<element_type, dynamic_extent> make_subspan(index_type offset, index_type count,
subspan_selector<dynamic_extent>) const
// clang-format on
constexpr span<element_type, dynamic_extent>
make_subspan(size_type offset, size_type count, subspan_selector<dynamic_extent>) const noexcept
{
Expects(offset >= 0 && size() - offset >= 0);
Expects(size() >= offset);
if (count == dynamic_extent) { return {KnownNotNull{data() + offset}, size() - offset}; }
Expects(count >= 0 && size() - offset >= count);
Expects(size() - offset >= count);
return {KnownNotNull{data() + offset}, count};
}
};
#if (defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L))
// Deduction Guides
template <class Type, std::size_t Extent>
span(Type (&)[Extent])->span<Type, Extent>;
template <class Type, std::size_t Size>
span(std::array<Type, Size>&)->span<Type, Size>;
template <class Type, std::size_t Size>
span(const std::array<Type, Size>&)->span<const Type, Size>;
#endif // ( defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201611L) )
#if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND)
template <class ElementType, std::ptrdiff_t Extent>
constexpr const typename span<ElementType, Extent>::index_type span<ElementType, Extent>::extent;
template <class ElementType, std::size_t Extent>
constexpr const typename span<ElementType, Extent>::size_type span<ElementType, Extent>::extent;
#endif
// [span.comparison], span comparison operators
template <class ElementType, std::ptrdiff_t FirstExtent, std::ptrdiff_t SecondExtent>
constexpr bool operator==(span<ElementType, FirstExtent> l, span<ElementType, SecondExtent> r)
{
return std::equal(l.begin(), l.end(), r.begin(), r.end());
}
template <class ElementType, std::ptrdiff_t Extent>
constexpr bool operator!=(span<ElementType, Extent> l, span<ElementType, Extent> r)
{
return !(l == r);
}
template <class ElementType, std::ptrdiff_t Extent>
constexpr bool operator<(span<ElementType, Extent> l, span<ElementType, Extent> r)
{
return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
}
template <class ElementType, std::ptrdiff_t Extent>
constexpr bool operator<=(span<ElementType, Extent> l, span<ElementType, Extent> r)
{
return !(l > r);
}
template <class ElementType, std::ptrdiff_t Extent>
constexpr bool operator>(span<ElementType, Extent> l, span<ElementType, Extent> r)
{
return r < l;
}
template <class ElementType, std::ptrdiff_t Extent>
constexpr bool operator>=(span<ElementType, Extent> l, span<ElementType, Extent> r)
{
return !(l < r);
}
namespace details
{
// if we only supported compilers with good constexpr support then
@@ -687,93 +705,41 @@ namespace details
// we should use a narrow_cast<> to go to std::size_t, but older compilers may not see it as
// constexpr
// and so will fail compilation of the template
template <class ElementType, std::ptrdiff_t Extent>
struct calculate_byte_size
: std::integral_constant<std::ptrdiff_t,
static_cast<std::ptrdiff_t>(sizeof(ElementType) *
static_cast<std::size_t>(Extent))>
template <class ElementType, std::size_t Extent>
struct calculate_byte_size : std::integral_constant<std::size_t, sizeof(ElementType) * Extent>
{
static_assert(Extent < dynamic_extent / sizeof(ElementType), "Size is too big.");
};
template <class ElementType>
struct calculate_byte_size<ElementType, dynamic_extent>
: std::integral_constant<std::ptrdiff_t, dynamic_extent>
: std::integral_constant<std::size_t, dynamic_extent>
{
};
} // namespace details
// [span.objectrep], views of object representation
template <class ElementType, std::ptrdiff_t Extent>
template <class ElementType, std::size_t Extent>
span<const byte, details::calculate_byte_size<ElementType, Extent>::value>
as_bytes(span<ElementType, Extent> s) noexcept
{
// clang-format off
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
// clang-format on
return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
}
template <class ElementType, std::ptrdiff_t Extent,
class = std::enable_if_t<!std::is_const<ElementType>::value>>
template <class ElementType, std::size_t Extent,
std::enable_if_t<!std::is_const<ElementType>::value, int> = 0>
span<byte, details::calculate_byte_size<ElementType, Extent>::value>
as_writeable_bytes(span<ElementType, Extent> s) noexcept
as_writable_bytes(span<ElementType, Extent> s) noexcept
{
// clang-format off
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
// clang-format on
return {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
}
//
// make_span() - Utility functions for creating spans
//
template <class ElementType>
constexpr span<ElementType> make_span(ElementType* ptr,
typename span<ElementType>::index_type count)
{
return span<ElementType>(ptr, count);
}
template <class ElementType>
constexpr span<ElementType> make_span(ElementType* firstElem, ElementType* lastElem)
{
return span<ElementType>(firstElem, lastElem);
}
template <class ElementType, std::size_t N>
constexpr span<ElementType, N> make_span(ElementType (&arr)[N]) noexcept
{
return span<ElementType, N>(arr);
}
template <class Container>
constexpr span<typename Container::value_type> make_span(Container& cont)
{
return span<typename Container::value_type>(cont);
}
template <class Container>
constexpr span<const typename Container::value_type> make_span(const Container& cont)
{
return span<const typename Container::value_type>(cont);
}
template <class Ptr>
constexpr span<typename Ptr::element_type> make_span(Ptr& cont, std::ptrdiff_t count)
{
return span<typename Ptr::element_type>(cont, count);
}
template <class Ptr>
constexpr span<typename Ptr::element_type> make_span(Ptr& cont)
{
return span<typename Ptr::element_type>(cont);
}
// Specialization of gsl::at for span
template <class ElementType, std::ptrdiff_t Extent>
constexpr ElementType& at(span<ElementType, Extent> s, index i)
{
// No bounds checking here because it is done in span::operator[] called below
return s[i];
}
} // namespace gsl
#if defined(_MSC_VER) && !defined(__clang__)
+198
View File
@@ -0,0 +1,198 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef GSL_SPAN_EXT_H
#define GSL_SPAN_EXT_H
///////////////////////////////////////////////////////////////////////////////
//
// File: span_ext
// Purpose: continue offering features that have been cut from the official
// implementation of span.
// While modernizing gsl::span a number of features needed to be removed to
// be compliant with the design of std::span
//
///////////////////////////////////////////////////////////////////////////////
#include <gsl/gsl_util> // for narrow_cast, narrow
#include <gsl/span> // for span
#include <algorithm> // for lexicographical_compare
#include <cstddef> // for ptrdiff_t, size_t
#include <utility>
namespace gsl
{
// [span.comparison], span comparison operators
template <class ElementType, std::size_t FirstExtent, std::size_t SecondExtent>
constexpr bool operator==(span<ElementType, FirstExtent> l, span<ElementType, SecondExtent> r)
{
return std::equal(l.begin(), l.end(), r.begin(), r.end());
}
template <class ElementType, std::size_t Extent>
constexpr bool operator!=(span<ElementType, Extent> l, span<ElementType, Extent> r)
{
return !(l == r);
}
template <class ElementType, std::size_t Extent>
constexpr bool operator<(span<ElementType, Extent> l, span<ElementType, Extent> r)
{
return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
}
template <class ElementType, std::size_t Extent>
constexpr bool operator<=(span<ElementType, Extent> l, span<ElementType, Extent> r)
{
return !(l > r);
}
template <class ElementType, std::size_t Extent>
constexpr bool operator>(span<ElementType, Extent> l, span<ElementType, Extent> r)
{
return r < l;
}
template <class ElementType, std::size_t Extent>
constexpr bool operator>=(span<ElementType, Extent> l, span<ElementType, Extent> r)
{
return !(l < r);
}
//
// make_span() - Utility functions for creating spans
//
template <class ElementType>
constexpr span<ElementType> make_span(ElementType* ptr, typename span<ElementType>::size_type count)
{
return span<ElementType>(ptr, count);
}
template <class ElementType>
constexpr span<ElementType> make_span(ElementType* firstElem, ElementType* lastElem)
{
return span<ElementType>(firstElem, lastElem);
}
template <class ElementType, std::size_t N>
constexpr span<ElementType, N> make_span(ElementType (&arr)[N]) noexcept
{
return span<ElementType, N>(arr);
}
template <class Container>
constexpr span<typename Container::value_type> make_span(Container& cont)
{
return span<typename Container::value_type>(cont);
}
template <class Container>
constexpr span<const typename Container::value_type> make_span(const Container& cont)
{
return span<const typename Container::value_type>(cont);
}
template <class Ptr>
constexpr span<typename Ptr::element_type> make_span(Ptr& cont, std::size_t count)
{
return span<typename Ptr::element_type>(cont, count);
}
template <class Ptr>
constexpr span<typename Ptr::element_type> make_span(Ptr& cont)
{
return span<typename Ptr::element_type>(cont);
}
// Specialization of gsl::at for span
template <class ElementType, std::size_t Extent>
constexpr ElementType& at(span<ElementType, Extent> s, index i)
{
// No bounds checking here because it is done in span::operator[] called below
Ensures(i >= 0);
return s[narrow_cast<std::size_t>(i)];
}
// [span.obs] Free observer functions
template <class ElementType, std::size_t Extent>
constexpr std::ptrdiff_t ssize(const span<ElementType, Extent>& s) noexcept
{
return static_cast<std::ptrdiff_t>(s.size());
}
// [span.iter] Free functions for begin/end functions
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::iterator
begin(const span<ElementType, Extent>& s) noexcept
{
return s.begin();
}
template <class ElementType, std::size_t Extent = dynamic_extent>
constexpr typename span<ElementType, Extent>::iterator
end(const span<ElementType, Extent>& s) noexcept
{
return s.end();
}
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::reverse_iterator
rbegin(const span<ElementType, Extent>& s) noexcept
{
return s.rbegin();
}
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::reverse_iterator
rend(const span<ElementType, Extent>& s) noexcept
{
return s.rend();
}
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::iterator
cbegin(const span<ElementType, Extent>& s) noexcept
{
return s.begin();
}
template <class ElementType, std::size_t Extent = dynamic_extent>
constexpr typename span<ElementType, Extent>::iterator
cend(const span<ElementType, Extent>& s) noexcept
{
return s.end();
}
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::reverse_iterator
crbegin(const span<ElementType, Extent>& s) noexcept
{
return s.rbegin();
}
template <class ElementType, std::size_t Extent>
constexpr typename span<ElementType, Extent>::reverse_iterator
crend(const span<ElementType, Extent>& s) noexcept
{
return s.rend();
}
} // namespace gsl
#endif // GSL_SPAN_EXT_H
+716 -721
View File
File diff suppressed because it is too large Load Diff
+110 -49
View File
@@ -5,27 +5,33 @@ project(GSLTests CXX)
# will make visual studio generated project group files
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
list(APPEND CATCH_CMAKE_ARGS
"-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/external"
"-DNO_SELFTEST=true"
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(
COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download
)
if(GIT_FOUND)
# add catch
ExternalProject_Add(
catch
PREFIX ${CMAKE_BINARY_DIR}/catch
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v2.0.1
CMAKE_ARGS ${CATCH_CMAKE_ARGS}
LOG_DOWNLOAD 1
UPDATE_DISCONNECTED 1
)
else()
# assume catch is installed in a system directory
add_custom_target(catch)
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(
COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download
)
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
add_subdirectory(
${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL
)
if (MSVC AND (GSL_CXX_STANDARD EQUAL 17))
set(GSL_CPLUSPLUS_OPT -Zc:__cplusplus -permissive-)
endif()
@@ -39,14 +45,38 @@ if(MSVC) # MSVC or simulating MSVC
/EHsc
/W4
/WX
$<$<CXX_COMPILER_ID:MSVC>:
/wd4996 # Use of function or classes marked [[deprecated]]
/wd26409 # CppCoreCheck - GTest
/wd26426 # CppCoreCheck - GTest
/wd26440 # CppCoreCheck - GTest
/wd26446 # CppCoreCheck - prefer gsl::at()
/wd26472 # CppCoreCheck - use gsl::narrow(_cast)
/wd26481 # CppCoreCheck - use span instead of pointer arithmetic
$<$<VERSION_LESS:$<CXX_COMPILER_VERSION>,1920>: # VS2015
/wd4189 # variable is initialized but not referenced
$<$<NOT:$<CONFIG:Debug>>: # Release, RelWithDebInfo
/wd4702 # Unreachable code
>
>
>
$<$<CXX_COMPILER_ID:Clang>:
-Weverything
-Wno-c++98-compat
-Wno-c++98-compat-pedantic
-Wno-covered-switch-default # GTest
-Wno-deprecated-declarations # Allow tests for [[deprecated]] elements
-Wno-global-constructors # GTest
-Wno-language-extension-token # GTest gtest-port.h
-Wno-missing-braces
-Wno-missing-prototypes
-Wno-unknown-attributes
$<$<EQUAL:${GSL_CXX_STANDARD},14>:-Wno-unused-member-function>
-Wno-shift-sign-overflow # GTest gtest-port.h
-Wno-undef # GTest
-Wno-used-but-marked-unused # GTest EXPECT_DEATH
$<$<EQUAL:${GSL_CXX_STANDARD},14>: # no support for [[maybe_unused]]
-Wno-unused-member-function
-Wno-unused-variable
>
>
)
else()
@@ -61,60 +91,77 @@ else()
-Wpedantic
-Wshadow
-Wsign-conversion
-Wno-deprecated-declarations # Allow tests for [[deprecated]] elements
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:
-Weverything
-Wno-c++98-compat
-Wno-c++98-compat-pedantic
-Wno-missing-braces
-Wno-covered-switch-default # GTest
-Wno-global-constructors # GTest
-Wno-missing-prototypes
-Wno-padded
-Wno-unknown-attributes
$<$<EQUAL:${GSL_CXX_STANDARD},14>:-Wno-unused-member-function>
-Wno-used-but-marked-unused # GTest EXPECT_DEATH
-Wno-weak-vtables
$<$<EQUAL:${GSL_CXX_STANDARD},14>: # no support for [[maybe_unused]]
-Wno-unused-member-function
-Wno-unused-variable
>
>
$<$<CXX_COMPILER_ID:Clang>:
$<$<CXX_COMPILER_VERSION:5.0.2>:-Wno-undefined-func-template>
$<$<AND:$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,4.99>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,6>>:
$<$<EQUAL:${GSL_CXX_STANDARD},17>:-Wno-undefined-func-template>
>
>
$<$<CXX_COMPILER_ID:AppleClang>:
$<$<AND:$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,9.1>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,10>>:
$<$<EQUAL:${GSL_CXX_STANDARD},17>:-Wno-undefined-func-template>
>
>
$<$<CXX_COMPILER_ID:GNU>:
-Wdouble-promotion # float implicit to double
-Wlogical-op # suspicious uses of logical operators
$<$<NOT:$<VERSION_LESS:$<CXX_COMPILER_VERSION>,6>>:
-Wduplicated-cond # duplicated if-else conditions
-Wmisleading-indentation
-Wnull-dereference
$<$<EQUAL:${GSL_CXX_STANDARD},14>: # no support for [[maybe_unused]]
-Wno-unused-variable
>
>
$<$<NOT:$<VERSION_LESS:$<CXX_COMPILER_VERSION>,7>>:
-Wduplicated-branches # identical if-else branches
>
>
)
endif(MSVC)
# for tests to find the catch header
target_include_directories(gsl_tests_config INTERFACE
${CMAKE_BINARY_DIR}/external/include
# for tests to find the gtest header
target_include_directories(gsl_tests_config SYSTEM INTERFACE
googletest/googletest/include
)
# set definitions for tests
target_compile_definitions(gsl_tests_config INTERFACE
GSL_THROW_ON_CONTRACT_VIOLATION
)
# create the main executable for each test. this reduces the compile time
# of each test by pre-compiling catch.
add_library(test_catch STATIC test.cpp)
target_link_libraries(test_catch
GSL
gsl_tests_config
)
add_dependencies(test_catch catch)
set_property(TARGET test_catch PROPERTY FOLDER "GSL_tests")
set_property(TARGET PROPERTY FOLDER "GSL_tests")
function(add_gsl_test name)
add_executable(${name} ${name}.cpp)
target_link_libraries(${name}
GSL
test_catch
gsl_tests_config
gtest_main
)
add_dependencies(${name} catch)
add_test(
${name}
${name}
${name}
${name}
)
# group all tests under GSL_tests
set_property(TARGET ${name} PROPERTY FOLDER "GSL_tests")
endfunction()
add_gsl_test(span_tests)
add_gsl_test(span_ext_tests)
add_gsl_test(span_compatibility_tests)
add_gsl_test(multi_span_tests)
add_gsl_test(strided_span_tests)
add_gsl_test(string_span_tests)
@@ -142,7 +189,7 @@ endforeach(flag_var)
add_library(gsl_tests_config_noexcept INTERFACE)
if(MSVC) # MSVC or simulating MSVC
target_compile_definitions(gsl_tests_config_noexcept INTERFACE
_HAS_EXCEPTIONS=0
_HAS_EXCEPTIONS=0 # disable exceptions in the Microsoft STL
)
target_compile_options(gsl_tests_config_noexcept INTERFACE
${GSL_CPLUSPLUS_OPT}
@@ -151,6 +198,8 @@ if(MSVC) # MSVC or simulating MSVC
$<$<CXX_COMPILER_ID:MSVC>:
/wd4577
/wd4702
/wd26440 # CppCoreCheck - GTest
/wd26446 # CppCoreCheck - prefer gsl::at()
>
$<$<CXX_COMPILER_ID:Clang>:
-Weverything
@@ -181,19 +230,31 @@ else()
-Wno-unknown-attributes
-Wno-weak-vtables
>
$<$<CXX_COMPILER_ID:GNU>:
-Wdouble-promotion # float implicit to double
-Wlogical-op # suspicious uses of logical operators
-Wuseless-cast # casting to its own type
$<$<NOT:$<VERSION_LESS:$<CXX_COMPILER_VERSION>,6>>:
-Wduplicated-cond # duplicated if-else conditions
-Wmisleading-indentation
-Wnull-dereference
>
$<$<NOT:$<VERSION_LESS:$<CXX_COMPILER_VERSION>,7>>:
-Wduplicated-branches # identical if-else branches
>
$<$<NOT:$<VERSION_LESS:$<CXX_COMPILER_VERSION>,8>>:
-Wcast-align=strict # increase alignment (i.e. char* to int*)
>
>
)
endif(MSVC)
# set definitions for tests
target_compile_definitions(gsl_tests_config_noexcept INTERFACE
GSL_TERMINATE_ON_CONTRACT_VIOLATION
)
function(add_gsl_test_noexcept name)
add_executable(${name} ${name}.cpp)
target_link_libraries(${name}
GSL
gsl_tests_config_noexcept
gtest_main
)
add_test(
${name}
+14
View File
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.0.2)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG 703bd9caab50b139428cea1aaff9974ebee5742e
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
+53 -47
View File
@@ -14,30 +14,26 @@
//
///////////////////////////////////////////////////////////////////////////////
#ifdef _MSC_VER
// blanket turn off warnings from CppCoreCheck from catch
// so people aren't annoyed by them when running the tool.
#pragma warning(disable : 26440 26426) // from catch
#endif
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, CHE...
#include <gtest/gtest.h>
#include <gsl/gsl_algorithm> // for copy
#include <gsl/span> // for span
#include <array> // for array
#include <cstddef> // for size_t
namespace gsl {
namespace
{
static constexpr char deathstring[] = "Expected Death";
}
namespace gsl
{
struct fail_fast;
} // namespace gsl
} // namespace gsl
using namespace std;
using namespace gsl;
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
TEST_CASE("same_type")
TEST(algorithm_tests, same_type)
{
// dynamic source and destination span
{
@@ -50,9 +46,10 @@ TEST_CASE("same_type")
copy(src_span, dst_span);
copy(src_span, dst_span.subspan(src_span.size()));
for (std::size_t i = 0; i < src.size(); ++i) {
CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]);
for (std::size_t i = 0; i < src.size(); ++i)
{
EXPECT_TRUE(dst[i] == src[i]);
EXPECT_TRUE(dst[i + src.size()] == src[i]);
}
}
@@ -67,9 +64,10 @@ TEST_CASE("same_type")
copy(src_span, dst_span);
copy(src_span, dst_span.subspan(src_span.size()));
for (std::size_t i = 0; i < src.size(); ++i) {
CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]);
for (std::size_t i = 0; i < src.size(); ++i)
{
EXPECT_TRUE(dst[i] == src[i]);
EXPECT_TRUE(dst[i + src.size()] == src[i]);
}
}
@@ -84,9 +82,10 @@ TEST_CASE("same_type")
copy(src_span, dst_span);
copy(src_span, dst_span.subspan(src_span.size()));
for (std::size_t i = 0; i < src.size(); ++i) {
CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]);
for (std::size_t i = 0; i < src.size(); ++i)
{
EXPECT_TRUE(dst[i] == src[i]);
EXPECT_TRUE(dst[i + src.size()] == src[i]);
}
}
@@ -101,17 +100,15 @@ TEST_CASE("same_type")
copy(src_span, dst_span);
copy(src_span, dst_span.subspan(src_span.size()));
for (std::size_t i = 0; i < src.size(); ++i) {
CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]);
for (std::size_t i = 0; i < src.size(); ++i)
{
EXPECT_TRUE(dst[i] == src[i]);
EXPECT_TRUE(dst[i + src.size()] == src[i]);
}
}
}
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
TEST_CASE("compatible_type")
TEST(algorithm_tests, compatible_type)
{
// dynamic source and destination span
{
@@ -124,9 +121,10 @@ TEST_CASE("compatible_type")
copy(src_span, dst_span);
copy(src_span, dst_span.subspan(src_span.size()));
for (std::size_t i = 0; i < src.size(); ++i) {
CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]);
for (std::size_t i = 0; i < src.size(); ++i)
{
EXPECT_TRUE(dst[i] == src[i]);
EXPECT_TRUE(dst[i + src.size()] == src[i]);
}
}
@@ -141,9 +139,10 @@ TEST_CASE("compatible_type")
copy(src_span, dst_span);
copy(src_span, dst_span.subspan(src_span.size()));
for (std::size_t i = 0; i < src.size(); ++i) {
CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]);
for (std::size_t i = 0; i < src.size(); ++i)
{
EXPECT_TRUE(dst[i] == src[i]);
EXPECT_TRUE(dst[i + src.size()] == src[i]);
}
}
@@ -158,9 +157,10 @@ TEST_CASE("compatible_type")
copy(src_span, dst_span);
copy(src_span, dst_span.subspan(src_span.size()));
for (std::size_t i = 0; i < src.size(); ++i) {
CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]);
for (std::size_t i = 0; i < src.size(); ++i)
{
EXPECT_TRUE(dst[i] == src[i]);
EXPECT_TRUE(dst[i + src.size()] == src[i]);
}
}
@@ -175,15 +175,16 @@ TEST_CASE("compatible_type")
copy(src_span, dst_span);
copy(src_span, dst_span.subspan(src_span.size()));
for (std::size_t i = 0; i < src.size(); ++i) {
CHECK(dst[i] == src[i]);
CHECK(dst[i + src.size()] == src[i]);
for (std::size_t i = 0; i < src.size(); ++i)
{
EXPECT_TRUE(dst[i] == src[i]);
EXPECT_TRUE(dst[i + src.size()] == src[i]);
}
}
}
#ifdef CONFIRM_COMPILATION_ERRORS
TEST_CASE("incompatible_type")
TEST(algorithm_tests, incompatible_type)
{
std::array<int, 4> src{1, 2, 3, 4};
std::array<int*, 12> dst{};
@@ -201,8 +202,13 @@ TEST_CASE("incompatible_type")
}
#endif
TEST_CASE("small_destination_span")
TEST(algorithm_tests, small_destination_span)
{
std::set_terminate([] {
std::cerr << "Expected Death. small_destination_span";
std::abort();
});
std::array<int, 12> src{1, 2, 3, 4};
std::array<int, 4> dst{};
@@ -211,9 +217,9 @@ TEST_CASE("small_destination_span")
const span<int> dst_span_dyn(dst);
const span<int, 4> dst_span_static(dst);
CHECK_THROWS_AS(copy(src_span_dyn, dst_span_dyn), fail_fast);
CHECK_THROWS_AS(copy(src_span_dyn, dst_span_static), fail_fast);
CHECK_THROWS_AS(copy(src_span_static, dst_span_dyn), fail_fast);
EXPECT_DEATH(copy(src_span_dyn, dst_span_dyn), deathstring);
EXPECT_DEATH(copy(src_span_dyn, dst_span_static), deathstring);
EXPECT_DEATH(copy(src_span_static, dst_span_dyn), deathstring);
#ifdef CONFIRM_COMPILATION_ERRORS
copy(src_span_static, dst_span_static);
+26 -17
View File
@@ -14,39 +14,48 @@
//
///////////////////////////////////////////////////////////////////////////////
#ifdef _MSC_VER
// blanket turn off warnings from CppCoreCheck from catch
// so people aren't annoyed by them when running the tool.
#pragma warning(disable : 26440 26426) // from catch
#endif
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, CHECK...
#include <gtest/gtest.h>
#include <gsl/gsl_assert> // for fail_fast (ptr only), Ensures, Expects
using namespace gsl;
namespace
{
static constexpr char deathstring[] = "Expected Death";
int f(int i)
{
Expects(i > 0 && i < 10);
return i;
}
TEST_CASE("expects")
{
CHECK(f(2) == 2);
CHECK_THROWS_AS(f(10), fail_fast);
}
int g(int i)
{
i++;
Ensures(i > 0 && i < 10);
return i;
}
} // namespace
TEST_CASE("ensures")
TEST(assertion_tests, expects)
{
CHECK(g(2) == 3);
CHECK_THROWS_AS(g(9), fail_fast);
std::set_terminate([] {
std::cerr << "Expected Death. expects";
std::abort();
});
EXPECT_TRUE(f(2) == 2);
EXPECT_DEATH(f(10), deathstring);
}
TEST(assertion_tests, ensures)
{
std::set_terminate([] {
std::cerr << "Expected Death. ensures";
std::abort();
});
EXPECT_TRUE(g(2) == 3);
EXPECT_DEATH(g(9), deathstring);
}
+53 -53
View File
@@ -14,13 +14,7 @@
//
///////////////////////////////////////////////////////////////////////////////
#ifdef _MSC_VER
// blanket turn off warnings from CppCoreCheck from catch
// so people aren't annoyed by them when running the tool.
#pragma warning(disable : 26440 26426) // from catch
#endif
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK_THROW...
#include <gtest/gtest.h>
#include <gsl/gsl_util> // for at
@@ -29,88 +23,95 @@
#include <initializer_list> // for initializer_list
#include <vector> // for vector
namespace
{
static constexpr char deathstring[] = "Expected Death";
}
namespace gsl {
struct fail_fast;
} // namespace gsl
using gsl::fail_fast;
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
TEST_CASE("static_array")
TEST(at_tests, static_array)
{
int a[4] = {1, 2, 3, 4};
const int(&c_a)[4] = a;
for (int i = 0; i < 4; ++i) {
CHECK(&gsl::at(a, i) == &a[i]);
CHECK(&gsl::at(c_a, i) == &a[i]);
EXPECT_TRUE(&gsl::at(a, i) == &a[i]);
EXPECT_TRUE(&gsl::at(c_a, i) == &a[i]);
}
CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast);
CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast);
std::set_terminate([] {
std::cerr << "Expected Death. static_array";
std::abort();
});
EXPECT_DEATH(gsl::at(a, -1), deathstring);
EXPECT_DEATH(gsl::at(a, 4), deathstring);
EXPECT_DEATH(gsl::at(c_a, -1), deathstring);
EXPECT_DEATH(gsl::at(c_a, 4), deathstring);
}
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
TEST_CASE("std_array")
TEST(at_tests, std_array)
{
std::array<int, 4> a = {1, 2, 3, 4};
const std::array<int, 4>& c_a = a;
for (int i = 0; i < 4; ++i) {
CHECK(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
EXPECT_TRUE(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
EXPECT_TRUE(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
}
CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast);
CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast);
std::set_terminate([] {
std::cerr << "Expected Death. std_array";
std::abort();
});
EXPECT_DEATH(gsl::at(a, -1), deathstring);
EXPECT_DEATH(gsl::at(a, 4), deathstring);
EXPECT_DEATH(gsl::at(c_a, -1), deathstring);
EXPECT_DEATH(gsl::at(c_a, 4), deathstring);
}
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
TEST_CASE("StdVector")
TEST(at_tests, std_vector)
{
std::vector<int> a = {1, 2, 3, 4};
const std::vector<int>& c_a = a;
for (int i = 0; i < 4; ++i) {
CHECK(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
CHECK(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
EXPECT_TRUE(&gsl::at(a, i) == &a[static_cast<std::size_t>(i)]);
EXPECT_TRUE(&gsl::at(c_a, i) == &a[static_cast<std::size_t>(i)]);
}
CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
CHECK_THROWS_AS(gsl::at(c_a, -1), fail_fast);
CHECK_THROWS_AS(gsl::at(c_a, 4), fail_fast);
std::set_terminate([] {
std::cerr << "Expected Death. std_vector";
std::abort();
});
EXPECT_DEATH(gsl::at(a, -1), deathstring);
EXPECT_DEATH(gsl::at(a, 4), deathstring);
EXPECT_DEATH(gsl::at(c_a, -1), deathstring);
EXPECT_DEATH(gsl::at(c_a, 4), deathstring);
}
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
TEST_CASE("InitializerList")
TEST(at_tests, InitializerList)
{
const std::initializer_list<int> a = {1, 2, 3, 4};
for (int i = 0; i < 4; ++i) {
CHECK(gsl::at(a, i) == i + 1);
CHECK(gsl::at({1, 2, 3, 4}, i) == i + 1);
EXPECT_TRUE(gsl::at(a, i) == i + 1);
EXPECT_TRUE(gsl::at({1, 2, 3, 4}, i) == i + 1);
}
CHECK_THROWS_AS(gsl::at(a, -1), fail_fast);
CHECK_THROWS_AS(gsl::at(a, 4), fail_fast);
CHECK_THROWS_AS(gsl::at({1, 2, 3, 4}, -1), fail_fast);
CHECK_THROWS_AS(gsl::at({1, 2, 3, 4}, 4), fail_fast);
std::set_terminate([] {
std::cerr << "Expected Death. InitializerList";
std::abort();
});
EXPECT_DEATH(gsl::at(a, -1), deathstring);
EXPECT_DEATH(gsl::at(a, 4), deathstring);
EXPECT_DEATH(gsl::at({1, 2, 3, 4}, -1), deathstring);
EXPECT_DEATH(gsl::at({1, 2, 3, 4}, 4), deathstring);
}
#if !defined(_MSC_VER) || defined(__clang__) || _MSC_VER >= 1910
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
static constexpr bool test_constexpr()
{
int a1[4] = {1, 2, 3, 4};
@@ -132,4 +133,3 @@ static constexpr bool test_constexpr()
static_assert(test_constexpr(), "FAIL");
#endif
+13 -36
View File
@@ -14,28 +14,12 @@
//
///////////////////////////////////////////////////////////////////////////////
#ifdef _MSC_VER
// blanket turn off warnings from CppCoreCheck from catch
// so people aren't annoyed by them when running the tool.
#pragma warning(disable : 26440 26426) // from catch
#pragma warning(disable : 4996) // use of function or classes marked [[deprecated]]
#endif
#if __clang__ || __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#include <catch/catch.hpp> // for AssertionHandler, StringRef, TEST_CASE
#include <gtest/gtest.h>
#include <gsl/multi_span> // for static_bounds, static_bounds_dynamic_range_t
#include <cstddef> // for ptrdiff_t, size_t
namespace gsl {
struct fail_fast;
} // namespace gsl
using namespace std;
using namespace gsl;
@@ -44,8 +28,7 @@ namespace
void use(std::ptrdiff_t&) {}
}
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
TEST_CASE("basic_bounds")
TEST(bounds_tests, basic_bounds)
{
for (auto point : static_bounds<dynamic_range, 3, 4>{2}) {
for (decltype(point)::size_type j = 0;
@@ -57,9 +40,7 @@ TEST_CASE("basic_bounds")
}
}
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("bounds_basic")
TEST(bounds_tests, bounds_basic)
{
static_bounds<3, 4, 5> b;
const auto a = b.slice();
@@ -68,9 +49,7 @@ TEST_CASE("bounds_basic")
x.slice().slice();
}
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("arrayview_iterator")
TEST(bounds_tests, arrayview_iterator)
{
static_bounds<4, dynamic_range, 2> bounds{3};
@@ -88,8 +67,7 @@ TEST_CASE("arrayview_iterator")
#endif
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("bounds_convertible")
TEST(bounds_tests, bounds_convertible)
{
static_bounds<7, 4, 2> b1;
static_bounds<7, dynamic_range, 2> b2 = b1;
@@ -102,24 +80,23 @@ TEST_CASE("bounds_convertible")
static_bounds<7, 4, 2> b4 = b3;
(void) b4;
static_bounds<dynamic_range> b11;
static_bounds<dynamic_range> b5;
static_bounds<34> b6;
std::set_terminate([] {
std::cerr << "Expected Death. bounds_convertible";
std::abort();
});
b5 = static_bounds<20>();
CHECK_THROWS_AS(b6 = b5, fail_fast);
EXPECT_DEATH(b6 = b5, ".*");
b5 = static_bounds<34>();
b6 = b5;
CHECK(b5 == b6);
CHECK(b5.size() == b6.size());
EXPECT_TRUE(b5 == b6);
EXPECT_TRUE(b5.size() == b6.size());
}
#ifdef CONFIRM_COMPILATION_ERRORS
copy(src_span_static, dst_span_static);
#endif
#if __clang__ || __GNUC__
#pragma GCC diagnostic pop
#endif
+89 -96
View File
@@ -14,13 +14,7 @@
//
///////////////////////////////////////////////////////////////////////////////
#ifdef _MSC_VER
// blanket turn off warnings from CppCoreCheck from catch
// so people aren't annoyed by them when running the tool.
#pragma warning(disable : 26440 26426) // from catch
#endif
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, TEST_...
#include <gtest/gtest.h>
#include <gsl/gsl_byte> // for to_byte, to_integer, byte, operator&, ope...
@@ -29,104 +23,103 @@ using namespace gsl;
namespace
{
TEST_CASE("construction")
{
{
const byte b = static_cast<byte>(4);
CHECK(static_cast<unsigned char>(b) == 4);
}
GSL_SUPPRESS(es.49)
{
const byte b = byte(12);
CHECK(static_cast<unsigned char>(b) == 12);
}
{
const byte b = to_byte<12>();
CHECK(static_cast<unsigned char>(b) == 12);
}
{
const unsigned char uc = 12;
const byte b = to_byte(uc);
CHECK(static_cast<unsigned char>(b) == 12);
}
#if defined(__cplusplus) && (__cplusplus >= 201703L)
{
const byte b { 14 };
CHECK(static_cast<unsigned char>(b) == 14);
}
#endif
}
TEST_CASE("bitwise_operations")
{
const byte b = to_byte<0xFF>();
byte a = to_byte<0x00>();
CHECK((b | a) == to_byte<0xFF>());
CHECK(a == to_byte<0x00>());
a |= b;
CHECK(a == to_byte<0xFF>());
a = to_byte<0x01>();
CHECK((b & a) == to_byte<0x01>());
a &= b;
CHECK(a == to_byte<0x01>());
CHECK((b ^ a) == to_byte<0xFE>());
CHECK(a == to_byte<0x01>());
a ^= b;
CHECK(a == to_byte<0xFE>());
a = to_byte<0x01>();
CHECK(~a == to_byte<0xFE>());
a = to_byte<0xFF>();
CHECK((a << 4) == to_byte<0xF0>());
CHECK((a >> 4) == to_byte<0x0F>());
a <<= 4;
CHECK(a == to_byte<0xF0>());
a >>= 4;
CHECK(a == to_byte<0x0F>());
}
TEST_CASE("to_integer")
{
const byte b = to_byte<0x12>();
CHECK(0x12 == gsl::to_integer<char>(b));
CHECK(0x12 == gsl::to_integer<short>(b));
CHECK(0x12 == gsl::to_integer<long>(b));
CHECK(0x12 == gsl::to_integer<long long>(b));
CHECK(0x12 == gsl::to_integer<unsigned char>(b));
CHECK(0x12 == gsl::to_integer<unsigned short>(b));
CHECK(0x12 == gsl::to_integer<unsigned long>(b));
CHECK(0x12 == gsl::to_integer<unsigned long long>(b));
// CHECK(0x12 == gsl::to_integer<float>(b)); // expect compile-time error
// CHECK(0x12 == gsl::to_integer<double>(b)); // expect compile-time error
}
int modify_both(gsl::byte & b, int& i)
int modify_both(gsl::byte& b, int& i)
{
i = 10;
b = to_byte<5>();
return i;
}
GSL_SUPPRESS(type.1)
TEST_CASE("aliasing")
TEST(byte_tests, construction)
{
{
const byte b = static_cast<byte>(4);
EXPECT_TRUE(static_cast<unsigned char>(b) == 4);
}
GSL_SUPPRESS(es.49)
{
const byte b = byte(12);
EXPECT_TRUE(static_cast<unsigned char>(b) == 12);
}
{
const byte b = to_byte<12>();
EXPECT_TRUE(static_cast<unsigned char>(b) == 12);
}
{
const unsigned char uc = 12;
const byte b = to_byte(uc);
EXPECT_TRUE(static_cast<unsigned char>(b) == 12);
}
#if defined(__cplusplus) && (__cplusplus >= 201703L)
{
const byte b { 14 };
EXPECT_TRUE(static_cast<unsigned char>(b) == 14);
}
#endif
}
TEST(byte_tests, bitwise_operations)
{
const byte b = to_byte<0xFF>();
byte a = to_byte<0x00>();
EXPECT_TRUE((b | a) == to_byte<0xFF>());
EXPECT_TRUE(a == to_byte<0x00>());
a |= b;
EXPECT_TRUE(a == to_byte<0xFF>());
a = to_byte<0x01>();
EXPECT_TRUE((b & a) == to_byte<0x01>());
a &= b;
EXPECT_TRUE(a == to_byte<0x01>());
EXPECT_TRUE((b ^ a) == to_byte<0xFE>());
EXPECT_TRUE(a == to_byte<0x01>());
a ^= b;
EXPECT_TRUE(a == to_byte<0xFE>());
a = to_byte<0x01>();
EXPECT_TRUE(~a == to_byte<0xFE>());
a = to_byte<0xFF>();
EXPECT_TRUE((a << 4) == to_byte<0xF0>());
EXPECT_TRUE((a >> 4) == to_byte<0x0F>());
a <<= 4;
EXPECT_TRUE(a == to_byte<0xF0>());
a >>= 4;
EXPECT_TRUE(a == to_byte<0x0F>());
}
TEST(byte_tests, to_integer)
{
const byte b = to_byte<0x12>();
EXPECT_TRUE(0x12 == gsl::to_integer<char>(b));
EXPECT_TRUE(0x12 == gsl::to_integer<short>(b));
EXPECT_TRUE(0x12 == gsl::to_integer<long>(b));
EXPECT_TRUE(0x12 == gsl::to_integer<long long>(b));
EXPECT_TRUE(0x12 == gsl::to_integer<unsigned char>(b));
EXPECT_TRUE(0x12 == gsl::to_integer<unsigned short>(b));
EXPECT_TRUE(0x12 == gsl::to_integer<unsigned long>(b));
EXPECT_TRUE(0x12 == gsl::to_integer<unsigned long long>(b));
// EXPECT_TRUE(0x12 == gsl::to_integer<float>(b)); // expect compile-time error
// EXPECT_TRUE(0x12 == gsl::to_integer<double>(b)); // expect compile-time error
}
TEST(byte_tests, aliasing)
{
int i{0};
const int res = modify_both(reinterpret_cast<byte&>(i), i);
CHECK(res == i);
EXPECT_TRUE(res == i);
}
}
+617 -550
View File
File diff suppressed because it is too large Load Diff
+118 -119
View File
@@ -14,16 +14,7 @@
//
///////////////////////////////////////////////////////////////////////////////
#ifdef _MSC_VER
// blanket turn off warnings from CppCoreCheck from catch
// so people aren't annoyed by them when running the tool.
#pragma warning(disable : 26440 26426) // from catch
// Fix VS2015 build breaks in Release
#pragma warning(disable : 4702) // unreachable code
#endif
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, TEST_...
#include <gtest/gtest.h>
#include <gsl/pointers> // for not_null, operator<, operator<=, operator>
@@ -34,13 +25,13 @@
#include <string> // for basic_string, operator==, string, operator<<
#include <typeinfo> // for type_info
namespace gsl
{
struct fail_fast;
} // namespace gsl
using namespace gsl;
namespace
{
static constexpr char deathstring[] = "Expected Death";
} //namespace
struct MyBase
{
};
@@ -133,10 +124,8 @@ GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
bool helper_const(not_null<const int*> p) { return *p == 12; }
int* return_pointer() { return nullptr; }
const int* return_pointer_const() { return nullptr; }
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("TestNotNullConstructors")
TEST(notnull_tests, TestNotNullConstructors)
{
{
#ifdef CONFIRM_COMPILATION_ERRORS
@@ -153,18 +142,22 @@ TEST_CASE("TestNotNullConstructors")
#endif
}
std::set_terminate([] {
std::cerr << "Expected Death. TestNotNullConstructors";
std::abort();
});
{
// from shared pointer
int i = 12;
auto rp = RefCounted<int>(&i);
not_null<int*> p(rp);
CHECK(p.get() == &i);
EXPECT_TRUE(p.get() == &i);
not_null<std::shared_ptr<int>> x(
std::make_shared<int>(10)); // shared_ptr<int> is nullptr assignable
int* pi = nullptr;
CHECK_THROWS_AS(not_null<decltype(pi)>(pi), fail_fast);
EXPECT_DEATH((not_null<decltype(pi)>(pi)), deathstring);
}
{
@@ -175,7 +168,7 @@ TEST_CASE("TestNotNullConstructors")
helper(&t);
helper_const(&t);
CHECK(*x == 42);
EXPECT_TRUE(*x == 42);
}
{
@@ -191,7 +184,7 @@ TEST_CASE("TestNotNullConstructors")
helper(x);
helper_const(x);
CHECK(*x == 42);
EXPECT_TRUE(*x == 42);
}
{
@@ -205,7 +198,7 @@ TEST_CASE("TestNotNullConstructors")
helper_const(cp);
helper_const(x);
CHECK(*x == 42);
EXPECT_TRUE(*x == 42);
}
{
@@ -215,19 +208,18 @@ TEST_CASE("TestNotNullConstructors")
auto x = not_null<const int*>{cp};
CHECK(*x == 42);
EXPECT_TRUE(*x == 42);
}
{
// from returned pointer
CHECK_THROWS_AS(helper(return_pointer()), fail_fast);
CHECK_THROWS_AS(helper_const(return_pointer()), fail_fast);
EXPECT_DEATH(helper(return_pointer()), deathstring);
EXPECT_DEATH(helper_const(return_pointer()), deathstring);
}
}
template <typename T>
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
void ostream_helper(T v)
{
not_null<T*> p(&v);
@@ -236,18 +228,18 @@ void ostream_helper(T v)
std::ostringstream ref;
os << static_cast<void*>(p);
ref << static_cast<void*>(&v);
CHECK(os.str() == ref.str());
EXPECT_TRUE(os.str() == ref.str());
}
{
std::ostringstream os;
std::ostringstream ref;
os << *p;
ref << v;
CHECK(os.str() == ref.str());
EXPECT_TRUE(os.str() == ref.str());
}
}
TEST_CASE("TestNotNullostream")
TEST(notnull_tests, TestNotNullostream)
{
ostream_helper<int>(17);
ostream_helper<float>(21.5f);
@@ -258,9 +250,7 @@ TEST_CASE("TestNotNullostream")
ostream_helper<std::string>("string");
}
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("TestNotNullCasting")
TEST(notnull_tests, TestNotNullCasting)
{
MyBase base;
MyDerived derived;
@@ -270,7 +260,7 @@ TEST_CASE("TestNotNullCasting")
not_null<MyDerived*> p{&derived};
not_null<MyBase*> q(&base);
q = p; // allowed with heterogeneous copy ctor
CHECK(q == p);
EXPECT_TRUE(q == p);
#ifdef CONFIRM_COMPILATION_ERRORS
q = u; // no viable conversion possible between MyBase* and Unrelated*
@@ -280,20 +270,25 @@ TEST_CASE("TestNotNullCasting")
not_null<Unrelated*> s = reinterpret_cast<Unrelated*>(p);
#endif
not_null<Unrelated*> t(reinterpret_cast<Unrelated*>(p.get()));
CHECK(reinterpret_cast<void*>(p.get()) == reinterpret_cast<void*>(t.get()));
EXPECT_TRUE(reinterpret_cast<void*>(p.get()) == reinterpret_cast<void*>(t.get()));
}
TEST_CASE("TestNotNullAssignment")
TEST(notnull_tests, TestNotNullAssignment)
{
std::set_terminate([] {
std::cerr << "Expected Death. TestNotNullAssignmentd";
std::abort();
});
int i = 12;
not_null<int*> p(&i);
CHECK(helper(p));
EXPECT_TRUE(helper(p));
int* q = nullptr;
CHECK_THROWS_AS(p = not_null<int*>(q), fail_fast);
EXPECT_DEATH(p = not_null<int*>(q), deathstring);
}
TEST_CASE("TestNotNullRawPointerComparison")
TEST(notnull_tests, TestNotNullRawPointerComparison)
{
int ints[2] = {42, 43};
int* p1 = &ints[0];
@@ -302,34 +297,33 @@ TEST_CASE("TestNotNullRawPointerComparison")
using NotNull1 = not_null<decltype(p1)>;
using NotNull2 = not_null<decltype(p2)>;
CHECK((NotNull1(p1) == NotNull1(p1)) == true);
CHECK((NotNull1(p1) == NotNull2(p2)) == false);
EXPECT_TRUE((NotNull1(p1) == NotNull1(p1)) == true);
EXPECT_TRUE((NotNull1(p1) == NotNull2(p2)) == false);
CHECK((NotNull1(p1) != NotNull1(p1)) == false);
CHECK((NotNull1(p1) != NotNull2(p2)) == true);
EXPECT_TRUE((NotNull1(p1) != NotNull1(p1)) == false);
EXPECT_TRUE((NotNull1(p1) != NotNull2(p2)) == true);
CHECK((NotNull1(p1) < NotNull1(p1)) == false);
CHECK((NotNull1(p1) < NotNull2(p2)) == (p1 < p2));
CHECK((NotNull2(p2) < NotNull1(p1)) == (p2 < p1));
EXPECT_TRUE((NotNull1(p1) < NotNull1(p1)) == false);
EXPECT_TRUE((NotNull1(p1) < NotNull2(p2)) == (p1 < p2));
EXPECT_TRUE((NotNull2(p2) < NotNull1(p1)) == (p2 < p1));
CHECK((NotNull1(p1) > NotNull1(p1)) == false);
CHECK((NotNull1(p1) > NotNull2(p2)) == (p1 > p2));
CHECK((NotNull2(p2) > NotNull1(p1)) == (p2 > p1));
EXPECT_TRUE((NotNull1(p1) > NotNull1(p1)) == false);
EXPECT_TRUE((NotNull1(p1) > NotNull2(p2)) == (p1 > p2));
EXPECT_TRUE((NotNull2(p2) > NotNull1(p1)) == (p2 > p1));
CHECK((NotNull1(p1) <= NotNull1(p1)) == true);
CHECK((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2));
CHECK((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1));
EXPECT_TRUE((NotNull1(p1) <= NotNull1(p1)) == true);
EXPECT_TRUE((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2));
EXPECT_TRUE((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1));
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("TestNotNullDereferenceOperator")
TEST(notnull_tests, TestNotNullDereferenceOperator)
{
{
auto sp1 = std::make_shared<NonCopyableNonMovable>();
using NotNullSp1 = not_null<decltype(sp1)>;
CHECK(typeid(*sp1) == typeid(*NotNullSp1(sp1)));
CHECK(std::addressof(*NotNullSp1(sp1)) == std::addressof(*sp1));
EXPECT_TRUE(typeid(*sp1) == typeid(*NotNullSp1(sp1)));
EXPECT_TRUE(std::addressof(*NotNullSp1(sp1)) == std::addressof(*sp1));
}
{
@@ -337,22 +331,22 @@ TEST_CASE("TestNotNullDereferenceOperator")
CustomPtr<int> p1(&ints[0]);
using NotNull1 = not_null<decltype(p1)>;
CHECK(typeid(*NotNull1(p1)) == typeid(*p1));
CHECK(*NotNull1(p1) == 42);
EXPECT_TRUE(typeid(*NotNull1(p1)) == typeid(*p1));
EXPECT_TRUE(*NotNull1(p1) == 42);
*NotNull1(p1) = 43;
CHECK(ints[0] == 43);
EXPECT_TRUE(ints[0] == 43);
}
{
int v = 42;
gsl::not_null<int*> p(&v);
CHECK(typeid(*p) == typeid(*(&v)));
EXPECT_TRUE(typeid(*p) == typeid(*(&v)));
*p = 43;
CHECK(v == 43);
EXPECT_TRUE(v == 43);
}
}
TEST_CASE("TestNotNullSharedPtrComparison")
TEST(notnull_tests, TestNotNullSharedPtrComparison)
{
auto sp1 = std::make_shared<int>(42);
auto sp2 = std::make_shared<const int>(43);
@@ -360,31 +354,30 @@ TEST_CASE("TestNotNullSharedPtrComparison")
using NotNullSp1 = not_null<decltype(sp1)>;
using NotNullSp2 = not_null<decltype(sp2)>;
CHECK((NotNullSp1(sp1) == NotNullSp1(sp1)) == true);
CHECK((NotNullSp1(sp1) == NotNullSp2(sp2)) == false);
EXPECT_TRUE((NotNullSp1(sp1) == NotNullSp1(sp1)) == true);
EXPECT_TRUE((NotNullSp1(sp1) == NotNullSp2(sp2)) == false);
CHECK((NotNullSp1(sp1) != NotNullSp1(sp1)) == false);
CHECK((NotNullSp1(sp1) != NotNullSp2(sp2)) == true);
EXPECT_TRUE((NotNullSp1(sp1) != NotNullSp1(sp1)) == false);
EXPECT_TRUE((NotNullSp1(sp1) != NotNullSp2(sp2)) == true);
CHECK((NotNullSp1(sp1) < NotNullSp1(sp1)) == false);
CHECK((NotNullSp1(sp1) < NotNullSp2(sp2)) == (sp1 < sp2));
CHECK((NotNullSp2(sp2) < NotNullSp1(sp1)) == (sp2 < sp1));
EXPECT_TRUE((NotNullSp1(sp1) < NotNullSp1(sp1)) == false);
EXPECT_TRUE((NotNullSp1(sp1) < NotNullSp2(sp2)) == (sp1 < sp2));
EXPECT_TRUE((NotNullSp2(sp2) < NotNullSp1(sp1)) == (sp2 < sp1));
CHECK((NotNullSp1(sp1) > NotNullSp1(sp1)) == false);
CHECK((NotNullSp1(sp1) > NotNullSp2(sp2)) == (sp1 > sp2));
CHECK((NotNullSp2(sp2) > NotNullSp1(sp1)) == (sp2 > sp1));
EXPECT_TRUE((NotNullSp1(sp1) > NotNullSp1(sp1)) == false);
EXPECT_TRUE((NotNullSp1(sp1) > NotNullSp2(sp2)) == (sp1 > sp2));
EXPECT_TRUE((NotNullSp2(sp2) > NotNullSp1(sp1)) == (sp2 > sp1));
CHECK((NotNullSp1(sp1) <= NotNullSp1(sp1)) == true);
CHECK((NotNullSp1(sp1) <= NotNullSp2(sp2)) == (sp1 <= sp2));
CHECK((NotNullSp2(sp2) <= NotNullSp1(sp1)) == (sp2 <= sp1));
EXPECT_TRUE((NotNullSp1(sp1) <= NotNullSp1(sp1)) == true);
EXPECT_TRUE((NotNullSp1(sp1) <= NotNullSp2(sp2)) == (sp1 <= sp2));
EXPECT_TRUE((NotNullSp2(sp2) <= NotNullSp1(sp1)) == (sp2 <= sp1));
CHECK((NotNullSp1(sp1) >= NotNullSp1(sp1)) == true);
CHECK((NotNullSp1(sp1) >= NotNullSp2(sp2)) == (sp1 >= sp2));
CHECK((NotNullSp2(sp2) >= NotNullSp1(sp1)) == (sp2 >= sp1));
EXPECT_TRUE((NotNullSp1(sp1) >= NotNullSp1(sp1)) == true);
EXPECT_TRUE((NotNullSp1(sp1) >= NotNullSp2(sp2)) == (sp1 >= sp2));
EXPECT_TRUE((NotNullSp2(sp2) >= NotNullSp1(sp1)) == (sp2 >= sp1));
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("TestNotNullCustomPtrComparison")
TEST(notnull_tests, TestNotNullCustomPtrComparison)
{
int ints[2] = {42, 43};
CustomPtr<int> p1(&ints[0]);
@@ -393,33 +386,32 @@ TEST_CASE("TestNotNullCustomPtrComparison")
using NotNull1 = not_null<decltype(p1)>;
using NotNull2 = not_null<decltype(p2)>;
CHECK((NotNull1(p1) == NotNull1(p1)) == "true");
CHECK((NotNull1(p1) == NotNull2(p2)) == "false");
EXPECT_TRUE((NotNull1(p1) == NotNull1(p1)) == "true");
EXPECT_TRUE((NotNull1(p1) == NotNull2(p2)) == "false");
CHECK((NotNull1(p1) != NotNull1(p1)) == "false");
CHECK((NotNull1(p1) != NotNull2(p2)) == "true");
EXPECT_TRUE((NotNull1(p1) != NotNull1(p1)) == "false");
EXPECT_TRUE((NotNull1(p1) != NotNull2(p2)) == "true");
CHECK((NotNull1(p1) < NotNull1(p1)) == "false");
CHECK((NotNull1(p1) < NotNull2(p2)) == (p1 < p2));
CHECK((NotNull2(p2) < NotNull1(p1)) == (p2 < p1));
EXPECT_TRUE((NotNull1(p1) < NotNull1(p1)) == "false");
EXPECT_TRUE((NotNull1(p1) < NotNull2(p2)) == (p1 < p2));
EXPECT_TRUE((NotNull2(p2) < NotNull1(p1)) == (p2 < p1));
CHECK((NotNull1(p1) > NotNull1(p1)) == "false");
CHECK((NotNull1(p1) > NotNull2(p2)) == (p1 > p2));
CHECK((NotNull2(p2) > NotNull1(p1)) == (p2 > p1));
EXPECT_TRUE((NotNull1(p1) > NotNull1(p1)) == "false");
EXPECT_TRUE((NotNull1(p1) > NotNull2(p2)) == (p1 > p2));
EXPECT_TRUE((NotNull2(p2) > NotNull1(p1)) == (p2 > p1));
CHECK((NotNull1(p1) <= NotNull1(p1)) == "true");
CHECK((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2));
CHECK((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1));
EXPECT_TRUE((NotNull1(p1) <= NotNull1(p1)) == "true");
EXPECT_TRUE((NotNull1(p1) <= NotNull2(p2)) == (p1 <= p2));
EXPECT_TRUE((NotNull2(p2) <= NotNull1(p1)) == (p2 <= p1));
CHECK((NotNull1(p1) >= NotNull1(p1)) == "true");
CHECK((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2));
CHECK((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1));
EXPECT_TRUE((NotNull1(p1) >= NotNull1(p1)) == "true");
EXPECT_TRUE((NotNull1(p1) >= NotNull2(p2)) == (p1 >= p2));
EXPECT_TRUE((NotNull2(p2) >= NotNull1(p1)) == (p2 >= p1));
}
#if defined(__cplusplus) && (__cplusplus >= 201703L)
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("TestNotNullConstructorTypeDeduction")
TEST(notnull_tests, TestNotNullConstructorTypeDeduction)
{
{
int i = 42;
@@ -428,7 +420,7 @@ TEST_CASE("TestNotNullConstructorTypeDeduction")
helper(not_null{&i});
helper_const(not_null{&i});
CHECK(*x == 42);
EXPECT_TRUE(*x == 42);
}
{
@@ -439,15 +431,20 @@ TEST_CASE("TestNotNullConstructorTypeDeduction")
helper(not_null{p});
helper_const(not_null{p});
CHECK(*x == 42);
EXPECT_TRUE(*x == 42);
}
std::set_terminate([] {
std::cerr << "Expected Death. TestNotNullConstructorTypeDeduction";
std::abort();
});
{
auto workaround_macro = []() {
int* p1 = nullptr;
const not_null x{p1};
};
CHECK_THROWS_AS(workaround_macro(), fail_fast);
EXPECT_DEATH(workaround_macro(), deathstring);
}
{
@@ -455,14 +452,14 @@ TEST_CASE("TestNotNullConstructorTypeDeduction")
const int* p1 = nullptr;
const not_null x{p1};
};
CHECK_THROWS_AS(workaround_macro(), fail_fast);
EXPECT_DEATH(workaround_macro(), deathstring);
}
{
int* p = nullptr;
CHECK_THROWS_AS(helper(not_null{p}), fail_fast);
CHECK_THROWS_AS(helper_const(not_null{p}), fail_fast);
EXPECT_DEATH(helper(not_null{p}), deathstring);
EXPECT_DEATH(helper_const(not_null{p}), deathstring);
}
#ifdef CONFIRM_COMPILATION_ERRORS
@@ -475,7 +472,7 @@ TEST_CASE("TestNotNullConstructorTypeDeduction")
}
#endif // #if defined(__cplusplus) && (__cplusplus >= 201703L)
TEST_CASE("TestMakeNotNull")
TEST(notnull_tests, TestMakeNotNull)
{
{
int i = 42;
@@ -484,7 +481,7 @@ TEST_CASE("TestMakeNotNull")
helper(make_not_null(&i));
helper_const(make_not_null(&i));
CHECK(*x == 42);
EXPECT_TRUE(*x == 42);
}
{
@@ -495,42 +492,44 @@ TEST_CASE("TestMakeNotNull")
helper(make_not_null(p));
helper_const(make_not_null(p));
CHECK(*x == 42);
EXPECT_TRUE(*x == 42);
}
std::set_terminate([] {
std::cerr << "Expected Death. TestMakeNotNull";
std::abort();
});
{
const auto workaround_macro = []() {
int* p1 = nullptr;
const auto x = make_not_null(p1);
CHECK(*x == 42);
EXPECT_TRUE(*x == 42);
};
CHECK_THROWS_AS(workaround_macro(), fail_fast);
EXPECT_DEATH(workaround_macro(), deathstring);
}
{
const auto workaround_macro = []() {
const int* p1 = nullptr;
const auto x = make_not_null(p1);
CHECK(*x == 42);
EXPECT_TRUE(*x == 42);
};
CHECK_THROWS_AS(workaround_macro(), fail_fast);
EXPECT_DEATH(workaround_macro(), deathstring);
}
{
int* p = nullptr;
CHECK_THROWS_AS(helper(make_not_null(p)), fail_fast);
CHECK_THROWS_AS(helper_const(make_not_null(p)), fail_fast);
EXPECT_DEATH(helper(make_not_null(p)), deathstring);
EXPECT_DEATH(helper_const(make_not_null(p)), deathstring);
}
#ifdef CONFIRM_COMPILATION_ERRORS
{
CHECK_THROWS_AS(make_not_null(nullptr), fail_fast);
CHECK_THROWS_AS(helper(make_not_null(nullptr)), fail_fast);
CHECK_THROWS_AS(helper_const(make_not_null(nullptr)), fail_fast);
EXPECT_DEATH(make_not_null(nullptr), deathstring);
EXPECT_DEATH(helper(make_not_null(nullptr)), deathstring);
EXPECT_DEATH(helper_const(make_not_null(nullptr)), deathstring);
}
#endif
}
static_assert(std::is_nothrow_move_constructible<not_null<void*>>::value,
"not_null must be no-throw move constructible");
+5 -15
View File
@@ -14,14 +14,7 @@
//
///////////////////////////////////////////////////////////////////////////////
#ifdef _MSC_VER
// blanket turn off warnings from CppCoreCheck from catch
// so people aren't annoyed by them when running the tool.
#pragma warning(disable : 26440 26426) // from catch
#endif
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, TEST_...
#include <gtest/gtest.h>
#include <gsl/pointers> // for owner
@@ -30,19 +23,16 @@ using namespace gsl;
GSL_SUPPRESS(f.23) // NO-FORMAT: attribute
void f(int* i) { *i += 1; }
GSL_SUPPRESS(r.11) // NO-FORMAT: attribute
GSL_SUPPRESS(r.3) // NO-FORMAT: attribute // TODO: false positive
GSL_SUPPRESS(r.5) // NO-FORMAT: attribute
TEST_CASE("basic_test")
TEST(owner_tests, basic_test)
{
owner<int*> p = new int(120);
CHECK(*p == 120);
EXPECT_TRUE(*p == 120);
f(p);
CHECK(*p == 121);
EXPECT_TRUE(*p == 121);
delete p;
}
TEST_CASE("check_pointer_constraint")
TEST(owner_tests, check_pointer_constraint)
{
#ifdef CONFIRM_COMPILATION_ERRORS
{
File diff suppressed because it is too large Load Diff
+360
View File
@@ -0,0 +1,360 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#include <gtest/gtest.h>
#include <gsl/gsl_util> // for narrow_cast, at
#include <gsl/span_ext> // for operator==, operator!=, make_span
#include <array> // for array
#include <iostream> // for cerr
#include <vector> // for vector
using namespace std;
using namespace gsl;
namespace
{
static constexpr char deathstring[] = "Expected Death";
} // namespace
TEST(span_ext_test, make_span_from_pointer_length_constructor)
{
std::set_terminate([] {
std::cerr << "Expected Death. from_pointer_length_constructor";
std::abort();
});
int arr[4] = {1, 2, 3, 4};
{
auto s = make_span(&arr[0], 2);
EXPECT_TRUE(s.size() == 2);
EXPECT_TRUE(s.data() == &arr[0]);
EXPECT_TRUE(s[0] == 1);
EXPECT_TRUE(s[1] == 2);
}
{
int* p = nullptr;
auto s = make_span(p, narrow_cast<span<int>::size_type>(0));
EXPECT_TRUE(s.size() == 0);
EXPECT_TRUE(s.data() == nullptr);
}
{
int* p = nullptr;
auto workaround_macro = [=]() { make_span(p, 2); };
EXPECT_DEATH(workaround_macro(), deathstring);
}
}
TEST(span_ext_test, make_span_from_pointer_pointer_construction)
{
int arr[4] = {1, 2, 3, 4};
{
auto s = make_span(&arr[0], &arr[2]);
EXPECT_TRUE(s.size() == 2);
EXPECT_TRUE(s.data() == &arr[0]);
EXPECT_TRUE(s[0] == 1);
EXPECT_TRUE(s[1] == 2);
}
{
auto s = make_span(&arr[0], &arr[0]);
EXPECT_TRUE(s.size() == 0);
EXPECT_TRUE(s.data() == &arr[0]);
}
{
int* p = nullptr;
auto s = make_span(p, p);
EXPECT_TRUE(s.size() == 0);
EXPECT_TRUE(s.data() == nullptr);
}
}
TEST(span_ext_test, make_span_from_array_constructor)
{
int arr[5] = {1, 2, 3, 4, 5};
int arr2d[2][3] = {1, 2, 3, 4, 5, 6};
int arr3d[2][3][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
{
const auto s = make_span(arr);
EXPECT_TRUE(s.size() == 5);
EXPECT_TRUE(s.data() == std::addressof(arr[0]));
}
{
const auto s = make_span(std::addressof(arr2d[0]), 1);
EXPECT_TRUE(s.size() == 1);
EXPECT_TRUE(s.data() == std::addressof(arr2d[0]));
}
{
const auto s = make_span(std::addressof(arr3d[0]), 1);
EXPECT_TRUE(s.size() == 1);
EXPECT_TRUE(s.data() == std::addressof(arr3d[0]));
}
}
TEST(span_ext_test, make_span_from_dynamic_array_constructor)
{
double(*arr)[3][4] = new double[100][3][4];
{
auto s = make_span(&arr[0][0][0], 10);
EXPECT_TRUE(s.size() == 10);
EXPECT_TRUE(s.data() == &arr[0][0][0]);
}
delete[] arr;
}
TEST(span_ext_test, make_span_from_std_array_constructor)
{
std::array<int, 4> arr = {1, 2, 3, 4};
{
auto s = make_span(arr);
EXPECT_TRUE(s.size() == arr.size());
EXPECT_TRUE(s.data() == arr.data());
}
// This test checks for the bug found in gcc 6.1, 6.2, 6.3, 6.4, 6.5 7.1, 7.2, 7.3 - issue #590
{
span<int> s1 = make_span(arr);
static span<int> s2;
s2 = s1;
#if defined(__GNUC__) && __GNUC__ == 6 && (__GNUC_MINOR__ == 4 || __GNUC_MINOR__ == 5) && \
__GNUC_PATCHLEVEL__ == 0 && defined(__OPTIMIZE__)
// Known to be broken in gcc 6.4 and 6.5 with optimizations
// Issue in gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83116
EXPECT_TRUE(s1.size() == 4);
EXPECT_TRUE(s2.size() == 0);
#else
EXPECT_TRUE(s1.size() == s2.size());
#endif
}
}
TEST(span_ext_test, make_span_from_const_std_array_constructor)
{
const std::array<int, 4> arr = {1, 2, 3, 4};
{
auto s = make_span(arr);
EXPECT_TRUE(s.size() == arr.size());
EXPECT_TRUE(s.data() == arr.data());
}
}
TEST(span_ext_test, make_span_from_std_array_const_constructor)
{
std::array<const int, 4> arr = {1, 2, 3, 4};
{
auto s = make_span(arr);
EXPECT_TRUE(s.size() == arr.size());
EXPECT_TRUE(s.data() == arr.data());
}
}
TEST(span_ext_test, make_span_from_container_constructor)
{
std::vector<int> v = {1, 2, 3};
const std::vector<int> cv = v;
{
auto s = make_span(v);
EXPECT_TRUE(s.size() == v.size());
EXPECT_TRUE(s.data() == v.data());
auto cs = make_span(cv);
EXPECT_TRUE(cs.size() == cv.size());
EXPECT_TRUE(cs.data() == cv.data());
}
}
TEST(span_test, interop_with_gsl_at)
{
int arr[5] = {1, 2, 3, 4, 5};
span<int> s{arr};
EXPECT_TRUE(at(s, 0) == 1);
EXPECT_TRUE(at(s, 1) == 2);
}
TEST(span_ext_test, iterator_free_functions)
{
int a[] = {1, 2, 3, 4};
span<int> s{a};
EXPECT_TRUE((std::is_same<decltype(s.begin()), decltype(begin(s))>::value));
EXPECT_TRUE((std::is_same<decltype(s.end()), decltype(end(s))>::value));
EXPECT_TRUE((std::is_same<decltype(std::cbegin(s)), decltype(cbegin(s))>::value));
EXPECT_TRUE((std::is_same<decltype(std::cend(s)), decltype(cend(s))>::value));
EXPECT_TRUE((std::is_same<decltype(s.rbegin()), decltype(rbegin(s))>::value));
EXPECT_TRUE((std::is_same<decltype(s.rend()), decltype(rend(s))>::value));
EXPECT_TRUE((std::is_same<decltype(std::crbegin(s)), decltype(crbegin(s))>::value));
EXPECT_TRUE((std::is_same<decltype(std::crend(s)), decltype(crend(s))>::value));
EXPECT_TRUE(s.begin() == begin(s));
EXPECT_TRUE(s.end() == end(s));
EXPECT_TRUE(s.rbegin() == rbegin(s));
EXPECT_TRUE(s.rend() == rend(s));
EXPECT_TRUE(s.begin() == cbegin(s));
EXPECT_TRUE(s.end() == cend(s));
EXPECT_TRUE(s.rbegin() == crbegin(s));
EXPECT_TRUE(s.rend() == crend(s));
}
TEST(span_ext_test, ssize_free_function)
{
int a[] = {1, 2, 3, 4};
span<int> s{a};
EXPECT_FALSE((std::is_same<decltype(s.size()), decltype(ssize(s))>::value));
EXPECT_TRUE(s.size() == static_cast<std::size_t>(ssize(s)));
}
TEST(span_ext_test, comparison_operators)
{
{
span<int> s1;
span<int> s2;
EXPECT_TRUE(s1 == s2);
EXPECT_FALSE(s1 != s2);
EXPECT_FALSE(s1 < s2);
EXPECT_TRUE(s1 <= s2);
EXPECT_FALSE(s1 > s2);
EXPECT_TRUE(s1 >= s2);
EXPECT_TRUE(s2 == s1);
EXPECT_FALSE(s2 != s1);
EXPECT_FALSE(s2 != s1);
EXPECT_TRUE(s2 <= s1);
EXPECT_FALSE(s2 > s1);
EXPECT_TRUE(s2 >= s1);
}
{
int arr[] = {2, 1};
span<int> s1 = arr;
span<int> s2 = arr;
EXPECT_TRUE(s1 == s2);
EXPECT_FALSE(s1 != s2);
EXPECT_FALSE(s1 < s2);
EXPECT_TRUE(s1 <= s2);
EXPECT_FALSE(s1 > s2);
EXPECT_TRUE(s1 >= s2);
EXPECT_TRUE(s2 == s1);
EXPECT_FALSE(s2 != s1);
EXPECT_FALSE(s2 < s1);
EXPECT_TRUE(s2 <= s1);
EXPECT_FALSE(s2 > s1);
EXPECT_TRUE(s2 >= s1);
}
{
int arr[] = {2, 1}; // bigger
span<int> s1;
span<int> s2 = arr;
EXPECT_TRUE(s1 != s2);
EXPECT_TRUE(s2 != s1);
EXPECT_FALSE(s1 == s2);
EXPECT_FALSE(s2 == s1);
EXPECT_TRUE(s1 < s2);
EXPECT_FALSE(s2 < s1);
EXPECT_TRUE(s1 <= s2);
EXPECT_FALSE(s2 <= s1);
EXPECT_TRUE(s2 > s1);
EXPECT_FALSE(s1 > s2);
EXPECT_TRUE(s2 >= s1);
EXPECT_FALSE(s1 >= s2);
}
{
int arr1[] = {1, 2};
int arr2[] = {1, 2};
span<int> s1 = arr1;
span<int> s2 = arr2;
EXPECT_TRUE(s1 == s2);
EXPECT_FALSE(s1 != s2);
EXPECT_FALSE(s1 < s2);
EXPECT_TRUE(s1 <= s2);
EXPECT_FALSE(s1 > s2);
EXPECT_TRUE(s1 >= s2);
EXPECT_TRUE(s2 == s1);
EXPECT_FALSE(s2 != s1);
EXPECT_FALSE(s2 < s1);
EXPECT_TRUE(s2 <= s1);
EXPECT_FALSE(s2 > s1);
EXPECT_TRUE(s2 >= s1);
}
{
int arr[] = {1, 2, 3};
span<int> s1 = {&arr[0], 2}; // shorter
span<int> s2 = arr; // longer
EXPECT_TRUE(s1 != s2);
EXPECT_TRUE(s2 != s1);
EXPECT_FALSE(s1 == s2);
EXPECT_FALSE(s2 == s1);
EXPECT_TRUE(s1 < s2);
EXPECT_FALSE(s2 < s1);
EXPECT_TRUE(s1 <= s2);
EXPECT_FALSE(s2 <= s1);
EXPECT_TRUE(s2 > s1);
EXPECT_FALSE(s1 > s2);
EXPECT_TRUE(s2 >= s1);
EXPECT_FALSE(s1 >= s2);
}
{
int arr1[] = {1, 2}; // smaller
int arr2[] = {2, 1}; // bigger
span<int> s1 = arr1;
span<int> s2 = arr2;
EXPECT_TRUE(s1 != s2);
EXPECT_TRUE(s2 != s1);
EXPECT_FALSE(s1 == s2);
EXPECT_FALSE(s2 == s1);
EXPECT_TRUE(s1 < s2);
EXPECT_FALSE(s2 < s1);
EXPECT_TRUE(s1 <= s2);
EXPECT_FALSE(s2 <= s1);
EXPECT_TRUE(s2 > s1);
EXPECT_FALSE(s1 > s2);
EXPECT_TRUE(s2 >= s1);
EXPECT_FALSE(s1 >= s2);
}
}
+1065 -1385
View File
File diff suppressed because it is too large Load Diff
+31 -34
View File
@@ -14,16 +14,7 @@
//
///////////////////////////////////////////////////////////////////////////////
#ifdef _MSC_VER
// blanket turn off warnings from CppCoreCheck from catch
// so people aren't annoyed by them when running the tool.
#pragma warning(disable : 26440 26426) // from catch
// Fix VS2015 build breaks in Release
#pragma warning(disable : 4702) // unreachable code
#endif
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, TEST_...
#include <gtest/gtest.h>
#include <gsl/pointers> // for not_null, operator<, operator<=, operator>
namespace gsl
@@ -35,19 +26,20 @@ using namespace gsl;
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
bool helper(not_null<int*> p) { return *p == 12; }
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
bool helper_const(not_null<const int*> p) { return *p == 12; }
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
bool strict_helper(strict_not_null<int*> p) { return *p == 12; }
GSL_SUPPRESS(f.4) // NO-FORMAT: attribute
bool strict_helper_const(strict_not_null<const int*> p) { return *p == 12; }
int* return_pointer() { return nullptr; }
const int* return_pointer_const() { return nullptr; }
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("TestStrictNotNull")
TEST(strict_notnull_tests, TestStrictNotNull)
{
{
// raw ptr <-> strict_not_null
@@ -66,7 +58,7 @@ TEST_CASE("TestStrictNotNull")
helper(snn1);
helper_const(snn1);
CHECK(*snn1 == 42);
EXPECT_TRUE(*snn1 == 42);
}
{
@@ -80,7 +72,7 @@ TEST_CASE("TestStrictNotNull")
strict_helper_const(snn1);
strict_helper_const(snn2);
CHECK(snn1 == snn2);
EXPECT_TRUE(snn1 == snn2);
}
{
@@ -95,8 +87,8 @@ TEST_CASE("TestStrictNotNull")
helper(snn);
helper_const(snn);
CHECK(snn == nn1);
CHECK(snn == nn2);
EXPECT_TRUE(snn == nn1);
EXPECT_TRUE(snn == nn2);
}
{
@@ -111,16 +103,16 @@ TEST_CASE("TestStrictNotNull")
strict_helper(nn);
strict_helper_const(nn);
CHECK(snn1 == nn);
CHECK(snn2 == nn);
EXPECT_TRUE(snn1 == nn);
EXPECT_TRUE(snn2 == nn);
std::hash<strict_not_null<int*>> hash_snn;
std::hash<not_null<int*>> hash_nn;
CHECK(hash_nn(snn1) == hash_nn(nn));
CHECK(hash_snn(snn1) == hash_nn(nn));
CHECK(hash_nn(snn1) == hash_nn(snn2));
CHECK(hash_snn(snn1) == hash_snn(nn));
EXPECT_TRUE(hash_nn(snn1) == hash_nn(nn));
EXPECT_TRUE(hash_snn(snn1) == hash_nn(nn));
EXPECT_TRUE(hash_nn(snn1) == hash_nn(snn2));
EXPECT_TRUE(hash_snn(snn1) == hash_snn(nn));
}
#ifdef CONFIRM_COMPILATION_ERRORS
@@ -131,10 +123,18 @@ TEST_CASE("TestStrictNotNull")
}
#if defined(__cplusplus) && (__cplusplus >= 201703L)
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("TestStrictNotNullConstructorTypeDeduction")
namespace
{
static constexpr char deathstring[] = "Expected Death";
}
TEST(strict_notnull_tests, TestStrictNotNullConstructorTypeDeduction)
{
std::set_terminate([] {
std::cerr << "Expected Death. TestStrictNotNullConstructorTypeDeduction";
std::abort();
});
{
int i = 42;
@@ -142,7 +142,7 @@ TEST_CASE("TestStrictNotNullConstructorTypeDeduction")
helper(strict_not_null{&i});
helper_const(strict_not_null{&i});
CHECK(*x == 42);
EXPECT_TRUE(*x == 42);
}
{
@@ -153,7 +153,7 @@ TEST_CASE("TestStrictNotNullConstructorTypeDeduction")
helper(strict_not_null{p});
helper_const(strict_not_null{p});
CHECK(*x == 42);
EXPECT_TRUE(*x == 42);
}
{
@@ -161,7 +161,7 @@ TEST_CASE("TestStrictNotNullConstructorTypeDeduction")
int* p1 = nullptr;
const strict_not_null x{p1};
};
CHECK_THROWS_AS(workaround_macro(), fail_fast);
EXPECT_DEATH(workaround_macro(), deathstring);
}
{
@@ -169,14 +169,14 @@ TEST_CASE("TestStrictNotNullConstructorTypeDeduction")
const int* p1 = nullptr;
const strict_not_null x{p1};
};
CHECK_THROWS_AS(workaround_macro(), fail_fast);
EXPECT_DEATH(workaround_macro(), deathstring);
}
{
int* p = nullptr;
CHECK_THROWS_AS(helper(strict_not_null{p}), fail_fast);
CHECK_THROWS_AS(helper_const(strict_not_null{p}), fail_fast);
EXPECT_DEATH(helper(strict_not_null{p}), deathstring);
EXPECT_DEATH(helper_const(strict_not_null{p}), deathstring);
}
#ifdef CONFIRM_COMPILATION_ERRORS
@@ -188,6 +188,3 @@ TEST_CASE("TestStrictNotNullConstructorTypeDeduction")
#endif
}
#endif // #if defined(__cplusplus) && (__cplusplus >= 201703L)
static_assert(std::is_nothrow_move_constructible<strict_not_null<void*>>::value,
"strict_not_null must be no-throw move constructible");
+262 -278
View File
@@ -14,21 +14,7 @@
//
///////////////////////////////////////////////////////////////////////////////
#ifdef _MSC_VER
// blanket turn off warnings from CppCoreCheck from catch
// so people aren't annoyed by them when running the tool.
#pragma warning(disable : 26440 26426) // from catch deprecated
#pragma warning(disable : 4996) // strided_span is in the process of being deprecated.
// Suppressing warnings until it is completely removed
#endif
#if __clang__ || __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, CHECK...
#include <gtest/gtest.h>
#include <gsl/gsl_byte> // for byte
#include <gsl/gsl_util> // for narrow_cast
#include <gsl/multi_span> // for strided_span, index, multi_span, strided_...
@@ -39,24 +25,80 @@
#include <type_traits> // for integral_constant<>::value, is_convertible
#include <vector> // for vector
namespace gsl {
struct fail_fast;
} // namespace gsl
using namespace std;
using namespace gsl;
namespace
{
static constexpr char deathstring[] = "Expected Death";
struct BaseClass
{
};
struct DerivedClass : BaseClass
{
};
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
void iterate_every_other_element(multi_span<int, dynamic_range> av)
{
// pick every other element
auto length = av.size() / 2;
#if defined(_MSC_VER) && _MSC_VER > 1800
auto bounds = strided_bounds<1>({length}, {2});
#else
auto bounds = strided_bounds<1>(multi_span_index<1>{length}, multi_span_index<1>{2});
#endif
strided_span<int, 1> strided(&av.data()[1], av.size() - 1, bounds);
EXPECT_TRUE(strided.size() == length);
EXPECT_TRUE(strided.bounds().index_bounds()[0] == length);
for (auto i = 0; i < strided.size(); ++i) {
EXPECT_TRUE(strided[i] == av[2 * i + 1]);
}
int idx = 0;
for (auto num : strided) {
EXPECT_TRUE(num == av[2 * idx + 1]);
idx++;
}
}
TEST_CASE("span_section_test")
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute // TODO: does not work
void iterate_second_slice(multi_span<int, dynamic_range, dynamic_range, dynamic_range> av)
{
const int expected[6] = {2, 3, 10, 11, 18, 19};
auto section = av.section({0, 1, 0}, {3, 1, 2});
for (auto i = 0; i < section.extent<0>(); ++i) {
for (auto j = 0; j < section.extent<1>(); ++j)
for (auto k = 0; k < section.extent<2>(); ++k) {
auto idx = multi_span_index<3>{i, j, k}; // avoid braces in the EXPECT_TRUE macro
EXPECT_TRUE(section[idx] == expected[2 * i + 2 * j + k]);
}
}
for (auto i = 0; i < section.extent<0>(); ++i) {
for (auto j = 0; j < section.extent<1>(); ++j)
for (auto k = 0; k < section.extent<2>(); ++k)
EXPECT_TRUE(section[i][j][k] == expected[2 * i + 2 * j + k]);
}
int i = 0;
for (const auto num : section) {
EXPECT_TRUE(num == expected[i]);
i++;
}
}
}
TEST(strided_span_tests, span_section_test)
{
int a[30][4][5];
@@ -66,51 +108,54 @@ TEST_CASE("span_section_test")
(void) subsub;
}
TEST_CASE("span_section")
TEST(strided_span_tests, span_section)
{
std::vector<int> data(5 * 10);
std::iota(begin(data), end(data), 0);
const multi_span<int, 5, 10> av = as_multi_span(multi_span<int>{data}, dim<5>(), dim<10>());
const strided_span<int, 2> av_section_1 = av.section({1, 2}, {3, 4});
CHECK(!av_section_1.empty());
CHECK((av_section_1[{0, 0}] == 12));
CHECK((av_section_1[{0, 1}] == 13));
CHECK((av_section_1[{1, 0}] == 22));
CHECK((av_section_1[{2, 3}] == 35));
EXPECT_TRUE(!av_section_1.empty());
EXPECT_TRUE((av_section_1[{0, 0}] == 12));
EXPECT_TRUE((av_section_1[{0, 1}] == 13));
EXPECT_TRUE((av_section_1[{1, 0}] == 22));
EXPECT_TRUE((av_section_1[{2, 3}] == 35));
const strided_span<int, 2> av_section_2 = av_section_1.section({1, 2}, {2, 2});
CHECK(!av_section_2.empty());
CHECK((av_section_2[{0, 0}] == 24));
CHECK((av_section_2[{0, 1}] == 25));
CHECK((av_section_2[{1, 0}] == 34));
EXPECT_TRUE(!av_section_2.empty());
EXPECT_TRUE((av_section_2[{0, 0}] == 24));
EXPECT_TRUE((av_section_2[{0, 1}] == 25));
EXPECT_TRUE((av_section_2[{1, 0}] == 34));
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("strided_span_constructors")
TEST(strided_span_tests, strided_span_constructors)
{
// Check stride constructor
// EXPECT_TRUE stride constructor
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
const int carr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
strided_span<int, 1> sav1{arr, {{9}, {1}}}; // T -> T
CHECK(sav1.bounds().index_bounds() == multi_span_index<1>{9});
CHECK(sav1.bounds().stride() == 1);
CHECK((sav1[0] == 1 && sav1[8] == 9));
EXPECT_TRUE(sav1.bounds().index_bounds() == multi_span_index<1>{9});
EXPECT_TRUE(sav1.bounds().stride() == 1);
EXPECT_TRUE(sav1[0] == 1);
EXPECT_TRUE(sav1[8] == 9);
strided_span<const int, 1> sav2{carr, {{4}, {2}}}; // const T -> const T
CHECK(sav2.bounds().index_bounds() == multi_span_index<1>{4});
CHECK(sav2.bounds().strides() == multi_span_index<1>{2});
CHECK((sav2[0] == 1 && sav2[3] == 7));
EXPECT_TRUE(sav2.bounds().index_bounds() == multi_span_index<1>{4});
EXPECT_TRUE(sav2.bounds().strides() == multi_span_index<1>{2});
EXPECT_TRUE(sav2[0] == 1);
EXPECT_TRUE(sav2[3] == 7);
strided_span<int, 2> sav3{arr, {{2, 2}, {6, 2}}}; // T -> const T
CHECK((sav3.bounds().index_bounds() == multi_span_index<2>{2, 2}));
CHECK((sav3.bounds().strides() == multi_span_index<2>{6, 2}));
CHECK((sav3[{0, 0}] == 1 && sav3[{0, 1}] == 3 && sav3[{1, 0}] == 7));
EXPECT_TRUE((sav3.bounds().index_bounds() == multi_span_index<2>{2, 2}));
EXPECT_TRUE((sav3.bounds().strides() == multi_span_index<2>{6, 2}));
EXPECT_TRUE((sav3[{0, 0}]) == 1);
EXPECT_TRUE((sav3[{0, 1}]) == 3);
EXPECT_TRUE((sav3[{1, 0}]) == 7);
}
// Check multi_span constructor
// EXPECT_TRUE multi_span constructor
{
int arr[] = {1, 2};
@@ -119,9 +164,9 @@ TEST_CASE("strided_span_constructors")
const multi_span<int> src = arr;
strided_span<int, 1> sav{src, {2, 1}};
CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2});
CHECK(sav.bounds().strides() == multi_span_index<1>{1});
CHECK(sav[1] == 2);
EXPECT_TRUE(sav.bounds().index_bounds() == multi_span_index<1>{2});
EXPECT_TRUE(sav.bounds().strides() == multi_span_index<1>{1});
EXPECT_TRUE(sav[1] == 2);
#if defined(_MSC_VER) && _MSC_VER > 1800
// strided_span<const int, 1> sav_c{ {src}, {2, 1} };
@@ -131,9 +176,9 @@ TEST_CASE("strided_span_constructors")
strided_span<const int, 1> sav_c{multi_span<const int>{src},
strided_bounds<1>{2, 1}};
#endif
CHECK(sav_c.bounds().index_bounds() == multi_span_index<1>{2});
CHECK(sav_c.bounds().strides() == multi_span_index<1>{1});
CHECK(sav_c[1] == 2);
EXPECT_TRUE(sav_c.bounds().index_bounds() == multi_span_index<1>{2});
EXPECT_TRUE(sav_c.bounds().strides() == multi_span_index<1>{1});
EXPECT_TRUE(sav_c[1] == 2);
#if defined(_MSC_VER) && _MSC_VER > 1800
strided_span<volatile int, 1> sav_v{src, {2, 1}};
@@ -141,9 +186,9 @@ TEST_CASE("strided_span_constructors")
strided_span<volatile int, 1> sav_v{multi_span<volatile int>{src},
strided_bounds<1>{2, 1}};
#endif
CHECK(sav_v.bounds().index_bounds() == multi_span_index<1>{2});
CHECK(sav_v.bounds().strides() == multi_span_index<1>{1});
CHECK(sav_v[1] == 2);
EXPECT_TRUE(sav_v.bounds().index_bounds() == multi_span_index<1>{2});
EXPECT_TRUE(sav_v.bounds().strides() == multi_span_index<1>{1});
EXPECT_TRUE(sav_v[1] == 2);
#if defined(_MSC_VER) && _MSC_VER > 1800
strided_span<const volatile int, 1> sav_cv{src, {2, 1}};
@@ -151,9 +196,9 @@ TEST_CASE("strided_span_constructors")
strided_span<const volatile int, 1> sav_cv{multi_span<const volatile int>{src},
strided_bounds<1>{2, 1}};
#endif
CHECK(sav_cv.bounds().index_bounds() == multi_span_index<1>{2});
CHECK(sav_cv.bounds().strides() == multi_span_index<1>{1});
CHECK(sav_cv[1] == 2);
EXPECT_TRUE(sav_cv.bounds().index_bounds() == multi_span_index<1>{2});
EXPECT_TRUE(sav_cv.bounds().strides() == multi_span_index<1>{1});
EXPECT_TRUE(sav_cv[1] == 2);
}
// From const-qualified source
@@ -161,9 +206,9 @@ TEST_CASE("strided_span_constructors")
const multi_span<const int> src{arr};
strided_span<const int, 1> sav_c{src, {2, 1}};
CHECK(sav_c.bounds().index_bounds() == multi_span_index<1>{2});
CHECK(sav_c.bounds().strides() == multi_span_index<1>{1});
CHECK(sav_c[1] == 2);
EXPECT_TRUE(sav_c.bounds().index_bounds() == multi_span_index<1>{2});
EXPECT_TRUE(sav_c.bounds().strides() == multi_span_index<1>{1});
EXPECT_TRUE(sav_c[1] == 2);
#if defined(_MSC_VER) && _MSC_VER > 1800
strided_span<const volatile int, 1> sav_cv{src, {2, 1}};
@@ -172,9 +217,9 @@ TEST_CASE("strided_span_constructors")
strided_bounds<1>{2, 1}};
#endif
CHECK(sav_cv.bounds().index_bounds() == multi_span_index<1>{2});
CHECK(sav_cv.bounds().strides() == multi_span_index<1>{1});
CHECK(sav_cv[1] == 2);
EXPECT_TRUE(sav_cv.bounds().index_bounds() == multi_span_index<1>{2});
EXPECT_TRUE(sav_cv.bounds().strides() == multi_span_index<1>{1});
EXPECT_TRUE(sav_cv[1] == 2);
}
// From volatile-qualified source
@@ -182,9 +227,9 @@ TEST_CASE("strided_span_constructors")
const multi_span<volatile int> src{arr};
strided_span<volatile int, 1> sav_v{src, {2, 1}};
CHECK(sav_v.bounds().index_bounds() == multi_span_index<1>{2});
CHECK(sav_v.bounds().strides() == multi_span_index<1>{1});
CHECK(sav_v[1] == 2);
EXPECT_TRUE(sav_v.bounds().index_bounds() == multi_span_index<1>{2});
EXPECT_TRUE(sav_v.bounds().strides() == multi_span_index<1>{1});
EXPECT_TRUE(sav_v[1] == 2);
#if defined(_MSC_VER) && _MSC_VER > 1800
strided_span<const volatile int, 1> sav_cv{src, {2, 1}};
@@ -192,9 +237,9 @@ TEST_CASE("strided_span_constructors")
strided_span<const volatile int, 1> sav_cv{multi_span<const volatile int>{src},
strided_bounds<1>{2, 1}};
#endif
CHECK(sav_cv.bounds().index_bounds() == multi_span_index<1>{2});
CHECK(sav_cv.bounds().strides() == multi_span_index<1>{1});
CHECK(sav_cv[1] == 2);
EXPECT_TRUE(sav_cv.bounds().index_bounds() == multi_span_index<1>{2});
EXPECT_TRUE(sav_cv.bounds().strides() == multi_span_index<1>{1});
EXPECT_TRUE(sav_cv[1] == 2);
}
// From cv-qualified source
@@ -202,19 +247,19 @@ TEST_CASE("strided_span_constructors")
const multi_span<const volatile int> src{arr};
strided_span<const volatile int, 1> sav_cv{src, {2, 1}};
CHECK(sav_cv.bounds().index_bounds() == multi_span_index<1>{2});
CHECK(sav_cv.bounds().strides() == multi_span_index<1>{1});
CHECK(sav_cv[1] == 2);
EXPECT_TRUE(sav_cv.bounds().index_bounds() == multi_span_index<1>{2});
EXPECT_TRUE(sav_cv.bounds().strides() == multi_span_index<1>{1});
EXPECT_TRUE(sav_cv[1] == 2);
}
}
// Check const-casting constructor
// EXPECT_TRUE const-casting constructor
{
int arr[2] = {4, 5};
const multi_span<int, 2> av(arr, 2);
multi_span<const int, 2> av2{av};
CHECK(av2[1] == 5);
EXPECT_TRUE(av2[1] == 5);
static_assert(
std::is_convertible<const multi_span<int, 2>, multi_span<const int, 2>>::value,
@@ -222,34 +267,35 @@ TEST_CASE("strided_span_constructors")
const strided_span<int, 1> src{arr, {2, 1}};
strided_span<const int, 1> sav{src};
CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2});
CHECK(sav.bounds().stride() == 1);
CHECK(sav[1] == 5);
EXPECT_TRUE(sav.bounds().index_bounds() == multi_span_index<1>{2});
EXPECT_TRUE(sav.bounds().stride() == 1);
EXPECT_TRUE(sav[1] == 5);
static_assert(
std::is_convertible<const strided_span<int, 1>, strided_span<const int, 1>>::value,
"ctor is not implicit!");
}
// Check copy constructor
// EXPECT_TRUE copy constructor
{
int arr1[2] = {3, 4};
const strided_span<int, 1> src1{arr1, {2, 1}};
strided_span<int, 1> sav1{src1};
CHECK(sav1.bounds().index_bounds() == multi_span_index<1>{2});
CHECK(sav1.bounds().stride() == 1);
CHECK(sav1[0] == 3);
EXPECT_TRUE(sav1.bounds().index_bounds() == multi_span_index<1>{2});
EXPECT_TRUE(sav1.bounds().stride() == 1);
EXPECT_TRUE(sav1[0] == 3);
int arr2[6] = {1, 2, 3, 4, 5, 6};
const strided_span<const int, 2> src2{arr2, {{3, 2}, {2, 1}}};
strided_span<const int, 2> sav2{src2};
CHECK((sav2.bounds().index_bounds() == multi_span_index<2>{3, 2}));
CHECK((sav2.bounds().strides() == multi_span_index<2>{2, 1}));
CHECK((sav2[{0, 0}] == 1 && sav2[{2, 0}] == 5));
EXPECT_TRUE((sav2.bounds().index_bounds() == multi_span_index<2>{3, 2}));
EXPECT_TRUE((sav2.bounds().strides() == multi_span_index<2>{2, 1}));
EXPECT_TRUE((sav2[{0, 0}]) == 1);
EXPECT_TRUE((sav2[{2, 0}]) == 5);
}
// Check const-casting assignment operator
// EXPECT_TRUE const-casting assignment operator
{
int arr1[2] = {1, 2};
int arr2[6] = {3, 4, 5, 6, 7, 8};
@@ -257,38 +303,38 @@ TEST_CASE("strided_span_constructors")
const strided_span<int, 1> src{arr1, {{2}, {1}}};
strided_span<const int, 1> sav{arr2, {{3}, {2}}};
strided_span<const int, 1>& sav_ref = (sav = src);
CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2});
CHECK(sav.bounds().strides() == multi_span_index<1>{1});
CHECK(sav[0] == 1);
CHECK(&sav_ref == &sav);
EXPECT_TRUE(sav.bounds().index_bounds() == multi_span_index<1>{2});
EXPECT_TRUE(sav.bounds().strides() == multi_span_index<1>{1});
EXPECT_TRUE(sav[0] == 1);
EXPECT_TRUE(&sav_ref == &sav);
}
// Check copy assignment operator
// EXPECT_TRUE copy assignment operator
{
int arr1[2] = {3, 4};
int arr1b[1] = {0};
const strided_span<int, 1> src1{arr1, {2, 1}};
strided_span<int, 1> sav1{arr1b, {1, 1}};
strided_span<int, 1>& sav1_ref = (sav1 = src1);
CHECK(sav1.bounds().index_bounds() == multi_span_index<1>{2});
CHECK(sav1.bounds().strides() == multi_span_index<1>{1});
CHECK(sav1[0] == 3);
CHECK(&sav1_ref == &sav1);
EXPECT_TRUE(sav1.bounds().index_bounds() == multi_span_index<1>{2});
EXPECT_TRUE(sav1.bounds().strides() == multi_span_index<1>{1});
EXPECT_TRUE(sav1[0] == 3);
EXPECT_TRUE(&sav1_ref == &sav1);
const int arr2[6] = {1, 2, 3, 4, 5, 6};
const int arr2b[1] = {0};
const strided_span<const int, 2> src2{arr2, {{3, 2}, {2, 1}}};
strided_span<const int, 2> sav2{arr2b, {{1, 1}, {1, 1}}};
strided_span<const int, 2>& sav2_ref = (sav2 = src2);
CHECK((sav2.bounds().index_bounds() == multi_span_index<2>{3, 2}));
CHECK((sav2.bounds().strides() == multi_span_index<2>{2, 1}));
CHECK((sav2[{0, 0}] == 1 && sav2[{2, 0}] == 5));
CHECK(&sav2_ref == &sav2);
EXPECT_TRUE((sav2.bounds().index_bounds() == multi_span_index<2>{3, 2}));
EXPECT_TRUE((sav2.bounds().strides() == multi_span_index<2>{2, 1}));
EXPECT_TRUE((sav2[{0, 0}] == 1));
EXPECT_TRUE((sav2[{2, 0}] == 5));
EXPECT_TRUE(&sav2_ref == &sav2);
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("strided_span_slice")
TEST(strided_span_tests, strided_span_slice)
{
std::vector<int> data(5 * 10);
std::iota(begin(data), end(data), 0);
@@ -303,19 +349,18 @@ TEST_CASE("strided_span_slice")
{{5, 10}, {10, 1}}};
strided_span<int, 1> sav_sl = sav[2];
CHECK(sav_sl[0] == 20);
CHECK(sav_sl[9] == 29);
EXPECT_TRUE(sav_sl[0] == 20);
EXPECT_TRUE(sav_sl[9] == 29);
strided_span<const int, 1> csav_sl = sav[3];
CHECK(csav_sl[0] == 30);
CHECK(csav_sl[9] == 39);
EXPECT_TRUE(csav_sl[0] == 30);
EXPECT_TRUE(csav_sl[9] == 39);
CHECK(sav[4][0] == 40);
CHECK(sav[4][9] == 49);
EXPECT_TRUE(sav[4][0] == 40);
EXPECT_TRUE(sav[4][9] == 49);
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("strided_span_column_major")
TEST(strided_span_tests, strided_span_column_major)
{
// strided_span may be used to accommodate more peculiar
// use cases, such as column-major multidimensional array
@@ -325,119 +370,123 @@ TEST_CASE("strided_span_column_major")
strided_span<int, 2> cm_sav{cm_array, {{5, 3}, {1, 5}}};
// Accessing elements
CHECK((cm_sav[{0, 0}] == 1));
CHECK((cm_sav[{0, 1}] == 2));
CHECK((cm_sav[{1, 0}] == 4));
CHECK((cm_sav[{4, 2}] == 15));
EXPECT_TRUE((cm_sav[{0, 0}] == 1));
EXPECT_TRUE((cm_sav[{0, 1}] == 2));
EXPECT_TRUE((cm_sav[{1, 0}] == 4));
EXPECT_TRUE((cm_sav[{4, 2}] == 15));
// Slice
strided_span<int, 1> cm_sl = cm_sav[3];
CHECK(cm_sl[0] == 10);
CHECK(cm_sl[1] == 11);
CHECK(cm_sl[2] == 12);
EXPECT_TRUE(cm_sl[0] == 10);
EXPECT_TRUE(cm_sl[1] == 11);
EXPECT_TRUE(cm_sl[2] == 12);
// Section
strided_span<int, 2> cm_sec = cm_sav.section({2, 1}, {3, 2});
CHECK((cm_sec.bounds().index_bounds() == multi_span_index<2>{3, 2}));
CHECK((cm_sec[{0, 0}] == 8));
CHECK((cm_sec[{0, 1}] == 9));
CHECK((cm_sec[{1, 0}] == 11));
CHECK((cm_sec[{2, 1}] == 15));
EXPECT_TRUE((cm_sec.bounds().index_bounds() == multi_span_index<2>{3, 2}));
EXPECT_TRUE((cm_sec[{0, 0}] == 8));
EXPECT_TRUE((cm_sec[{0, 1}] == 9));
EXPECT_TRUE((cm_sec[{1, 0}] == 11));
EXPECT_TRUE((cm_sec[{2, 1}] == 15));
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("strided_span_bounds")
TEST(strided_span_tests, strided_span_bounds)
{
int arr[] = {0, 1, 2, 3};
multi_span<int> av(arr);
std::set_terminate([] {
std::cerr << "Expected Death. strided_span_bounds";
std::abort();
});
{
// incorrect sections
CHECK_THROWS_AS(av.section(0, 0)[0], fail_fast);
CHECK_THROWS_AS(av.section(1, 0)[0], fail_fast);
CHECK_THROWS_AS(av.section(1, 1)[1], fail_fast);
EXPECT_DEATH(av.section(0, 0)[0], deathstring);
EXPECT_DEATH(av.section(1, 0)[0], deathstring);
EXPECT_DEATH(av.section(1, 1)[1], deathstring);
CHECK_THROWS_AS(av.section(2, 5), fail_fast);
CHECK_THROWS_AS(av.section(5, 2), fail_fast);
CHECK_THROWS_AS(av.section(5, 0), fail_fast);
CHECK_THROWS_AS(av.section(0, 5), fail_fast);
CHECK_THROWS_AS(av.section(5, 5), fail_fast);
EXPECT_DEATH(av.section(2, 5), deathstring);
EXPECT_DEATH(av.section(5, 2), deathstring);
EXPECT_DEATH(av.section(5, 0), deathstring);
EXPECT_DEATH(av.section(0, 5), deathstring);
EXPECT_DEATH(av.section(5, 5), deathstring);
}
{
// zero stride
strided_span<int, 1> sav{av, {{4}, {}}};
CHECK(sav[0] == 0);
CHECK(sav[3] == 0);
CHECK_THROWS_AS(sav[4], fail_fast);
EXPECT_TRUE(sav[0] == 0);
EXPECT_TRUE(sav[3] == 0);
EXPECT_DEATH(sav[4], deathstring);
}
{
// zero extent
strided_span<int, 1> sav{av, {{}, {1}}};
CHECK_THROWS_AS(sav[0], fail_fast);
EXPECT_DEATH(sav[0], deathstring);
}
{
// zero extent and stride
strided_span<int, 1> sav{av, {{}, {}}};
CHECK_THROWS_AS(sav[0], fail_fast);
EXPECT_DEATH(sav[0], deathstring);
}
{
// strided array ctor with matching strided bounds
strided_span<int, 1> sav{arr, {4, 1}};
CHECK(sav.bounds().index_bounds() == multi_span_index<1>{4});
CHECK(sav[3] == 3);
CHECK_THROWS_AS(sav[4], fail_fast);
EXPECT_TRUE(sav.bounds().index_bounds() == multi_span_index<1>{4});
EXPECT_TRUE(sav[3] == 3);
EXPECT_DEATH(sav[4], deathstring);
}
{
// strided array ctor with smaller strided bounds
strided_span<int, 1> sav{arr, {2, 1}};
CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2});
CHECK(sav[1] == 1);
CHECK_THROWS_AS(sav[2], fail_fast);
EXPECT_TRUE(sav.bounds().index_bounds() == multi_span_index<1>{2});
EXPECT_TRUE(sav[1] == 1);
EXPECT_DEATH(sav[2], deathstring);
}
{
// strided array ctor with fitting irregular bounds
strided_span<int, 1> sav{arr, {2, 3}};
CHECK(sav.bounds().index_bounds() == multi_span_index<1>{2});
CHECK(sav[0] == 0);
CHECK(sav[1] == 3);
CHECK_THROWS_AS(sav[2], fail_fast);
EXPECT_TRUE(sav.bounds().index_bounds() == multi_span_index<1>{2});
EXPECT_TRUE(sav[0] == 0);
EXPECT_TRUE(sav[1] == 3);
EXPECT_DEATH(sav[2], deathstring);
}
{
// bounds cross data boundaries - from static arrays
CHECK_THROWS_AS((strided_span<int, 1>{arr, {3, 2}}), fail_fast);
CHECK_THROWS_AS((strided_span<int, 1>{arr, {3, 3}}), fail_fast);
CHECK_THROWS_AS((strided_span<int, 1>{arr, {4, 5}}), fail_fast);
CHECK_THROWS_AS((strided_span<int, 1>{arr, {5, 1}}), fail_fast);
CHECK_THROWS_AS((strided_span<int, 1>{arr, {5, 5}}), fail_fast);
EXPECT_DEATH((strided_span<int, 1>{arr, {3, 2}}), deathstring);
EXPECT_DEATH((strided_span<int, 1>{arr, {3, 3}}), deathstring);
EXPECT_DEATH((strided_span<int, 1>{arr, {4, 5}}), deathstring);
EXPECT_DEATH((strided_span<int, 1>{arr, {5, 1}}), deathstring);
EXPECT_DEATH((strided_span<int, 1>{arr, {5, 5}}), deathstring);
}
{
// bounds cross data boundaries - from array view
CHECK_THROWS_AS((strided_span<int, 1>{av, {3, 2}}), fail_fast);
CHECK_THROWS_AS((strided_span<int, 1>{av, {3, 3}}), fail_fast);
CHECK_THROWS_AS((strided_span<int, 1>{av, {4, 5}}), fail_fast);
CHECK_THROWS_AS((strided_span<int, 1>{av, {5, 1}}), fail_fast);
CHECK_THROWS_AS((strided_span<int, 1>{av, {5, 5}}), fail_fast);
EXPECT_DEATH((strided_span<int, 1>{av, {3, 2}}), deathstring);
EXPECT_DEATH((strided_span<int, 1>{av, {3, 3}}), deathstring);
EXPECT_DEATH((strided_span<int, 1>{av, {4, 5}}), deathstring);
EXPECT_DEATH((strided_span<int, 1>{av, {5, 1}}), deathstring);
EXPECT_DEATH((strided_span<int, 1>{av, {5, 5}}), deathstring);
}
{
// bounds cross data boundaries - from dynamic arrays
CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {3, 2}}), fail_fast);
CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {3, 3}}), fail_fast);
CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {4, 5}}), fail_fast);
CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {5, 1}}), fail_fast);
CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 4, {5, 5}}), fail_fast);
CHECK_THROWS_AS((strided_span<int, 1>{av.data(), 2, {2, 2}}), fail_fast);
EXPECT_DEATH((strided_span<int, 1>{av.data(), 4, {3, 2}}), deathstring);
EXPECT_DEATH((strided_span<int, 1>{av.data(), 4, {3, 3}}), deathstring);
EXPECT_DEATH((strided_span<int, 1>{av.data(), 4, {4, 5}}), deathstring);
EXPECT_DEATH((strided_span<int, 1>{av.data(), 4, {5, 1}}), deathstring);
EXPECT_DEATH((strided_span<int, 1>{av.data(), 4, {5, 5}}), deathstring);
EXPECT_DEATH((strided_span<int, 1>{av.data(), 2, {2, 2}}), deathstring);
}
#ifdef CONFIRM_COMPILATION_ERRORS
@@ -464,12 +513,16 @@ TEST_CASE("strided_span_bounds")
#endif
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("strided_span_type_conversion")
TEST(strided_span_tests, strided_span_type_conversion)
{
int arr[] = {0, 1, 2, 3};
multi_span<int> av(arr);
std::set_terminate([] {
std::cerr << "Expected Death. strided_span_type_conversion";
std::abort();
});
{
strided_span<int, 1> sav{av.data(), av.size(), {av.size() / 2, 2}};
#ifdef CONFIRM_COMPILATION_ERRORS
@@ -490,10 +543,10 @@ TEST_CASE("strided_span_type_conversion")
strided_bounds<2> bounds{{2, bytes.size() / 4}, {bytes.size() / 2, 1}};
strided_span<const byte, 2> sav2{bytes.data(), bytes.size(), bounds};
strided_span<const int, 2> sav3 = sav2.as_strided_span<const int>();
CHECK(sav3[0][0] == 0);
CHECK(sav3[1][0] == 2);
CHECK_THROWS_AS(sav3[1][1], fail_fast);
CHECK_THROWS_AS(sav3[0][1], fail_fast);
EXPECT_TRUE(sav3[0][0] == 0);
EXPECT_TRUE(sav3[1][0] == 2);
EXPECT_DEATH(sav3[1][1], deathstring);
EXPECT_DEATH(sav3[0][1], deathstring);
}
// retype strided array with regular strides - from multi_span
@@ -503,10 +556,10 @@ TEST_CASE("strided_span_type_conversion")
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
strided_span<const byte, 2> sav2{bytes2, bounds};
strided_span<int, 2> sav3 = sav2.as_strided_span<int>();
CHECK(sav3[0][0] == 0);
CHECK(sav3[1][0] == 2);
CHECK_THROWS_AS(sav3[1][1], fail_fast);
CHECK_THROWS_AS(sav3[0][1], fail_fast);
EXPECT_TRUE(sav3[0][0] == 0);
EXPECT_TRUE(sav3[1][0] == 2);
EXPECT_DEATH(sav3[1][1], deathstring);
EXPECT_DEATH(sav3[0][1], deathstring);
}
// retype strided array with not enough elements - last dimension of the array is too small
@@ -515,7 +568,7 @@ TEST_CASE("strided_span_type_conversion")
multi_span<const byte, 2, dynamic_range> bytes2 =
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
strided_span<const byte, 2> sav2{bytes2, bounds};
CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast);
EXPECT_DEATH(sav2.as_strided_span<int>(), deathstring);
}
// retype strided array with not enough elements - strides are too small
@@ -524,7 +577,7 @@ TEST_CASE("strided_span_type_conversion")
multi_span<const byte, 2, dynamic_range> bytes2 =
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
strided_span<const byte, 2> sav2{bytes2, bounds};
CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast);
EXPECT_DEATH(sav2.as_strided_span<int>(), deathstring);
}
// retype strided array with not enough elements - last dimension does not divide by the new
@@ -534,7 +587,7 @@ TEST_CASE("strided_span_type_conversion")
multi_span<const byte, 2, dynamic_range> bytes2 =
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
strided_span<const byte, 2> sav2{bytes2, bounds};
CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast);
EXPECT_DEATH(sav2.as_strided_span<int>(), deathstring);
}
// retype strided array with not enough elements - strides does not divide by the new
@@ -544,88 +597,63 @@ TEST_CASE("strided_span_type_conversion")
multi_span<const byte, 2, dynamic_range> bytes2 =
as_multi_span(bytes, dim<2>(), dim(bytes.size() / 2));
strided_span<const byte, 2> sav2{bytes2, bounds};
CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast);
EXPECT_DEATH(sav2.as_strided_span<int>(), deathstring);
}
// retype strided array with irregular strides - from raw data
{
strided_bounds<1> bounds{bytes.size() / 2, 2};
strided_span<const byte, 1> sav2{bytes.data(), bytes.size(), bounds};
CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast);
EXPECT_DEATH(sav2.as_strided_span<int>(), deathstring);
}
// retype strided array with irregular strides - from multi_span
{
strided_bounds<1> bounds{bytes.size() / 2, 2};
strided_span<const byte, 1> sav2{bytes, bounds};
CHECK_THROWS_AS(sav2.as_strided_span<int>(), fail_fast);
EXPECT_DEATH(sav2.as_strided_span<int>(), deathstring);
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
TEST_CASE("empty_strided_spans")
TEST(strided_span_tests, empty_strided_spans)
{
std::set_terminate([] {
std::cerr << "Expected Death. empty_strided_spans";
std::abort();
});
{
multi_span<int, 0> empty_av(nullptr);
strided_span<int, 1> empty_sav{empty_av, {0, 1}};
CHECK(empty_sav.bounds().index_bounds() == multi_span_index<1>{0});
CHECK(empty_sav.empty());
CHECK_THROWS_AS(empty_sav[0], fail_fast);
CHECK_THROWS_AS(empty_sav.begin()[0], fail_fast);
CHECK_THROWS_AS(empty_sav.cbegin()[0], fail_fast);
EXPECT_TRUE(empty_sav.bounds().index_bounds() == multi_span_index<1>{0});
EXPECT_TRUE(empty_sav.empty());
EXPECT_DEATH(empty_sav[0], deathstring);
EXPECT_DEATH(empty_sav.begin()[0], deathstring);
EXPECT_DEATH(empty_sav.cbegin()[0], deathstring);
for (const auto& v : empty_sav) {
(void) v;
CHECK(false);
EXPECT_TRUE(false);
}
}
{
strided_span<int, 1> empty_sav{nullptr, 0, {0, 1}};
CHECK(empty_sav.bounds().index_bounds() == multi_span_index<1>{0});
CHECK_THROWS_AS(empty_sav[0], fail_fast);
CHECK_THROWS_AS(empty_sav.begin()[0], fail_fast);
CHECK_THROWS_AS(empty_sav.cbegin()[0], fail_fast);
EXPECT_TRUE(empty_sav.bounds().index_bounds() == multi_span_index<1>{0});
EXPECT_DEATH(empty_sav[0], deathstring);
EXPECT_DEATH(empty_sav.begin()[0], deathstring);
EXPECT_DEATH(empty_sav.cbegin()[0], deathstring);
for (const auto& v : empty_sav) {
(void) v;
CHECK(false);
EXPECT_TRUE(false);
}
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
void iterate_every_other_element(multi_span<int, dynamic_range> av)
{
// pick every other element
auto length = av.size() / 2;
#if defined(_MSC_VER) && _MSC_VER > 1800
auto bounds = strided_bounds<1>({length}, {2});
#else
auto bounds = strided_bounds<1>(multi_span_index<1>{length}, multi_span_index<1>{2});
#endif
strided_span<int, 1> strided(&av.data()[1], av.size() - 1, bounds);
CHECK(strided.size() == length);
CHECK(strided.bounds().index_bounds()[0] == length);
for (auto i = 0; i < strided.size(); ++i) {
CHECK(strided[i] == av[2 * i + 1]);
}
int idx = 0;
for (auto num : strided) {
CHECK(num == av[2 * idx + 1]);
idx++;
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("strided_span_section_iteration")
TEST(strided_span_tests, strided_span_section_iteration)
{
int arr[8] = {4, 0, 5, 1, 6, 2, 7, 3};
@@ -642,12 +670,7 @@ TEST_CASE("strided_span_section_iteration")
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
GSL_SUPPRESS(r.11) // NO-FORMAT: attribute
GSL_SUPPRESS(r.3) // NO-FORMAT: attribute
GSL_SUPPRESS(r.5) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
TEST_CASE("dynamic_strided_span_section_iteration")
TEST(strided_span_tests, dynamic_strided_span_section_iteration)
{
auto arr = new int[8];
for (int i = 0; i < 4; ++i) {
@@ -661,39 +684,7 @@ TEST_CASE("dynamic_strided_span_section_iteration")
delete[] arr;
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute // TODO: does not work
void iterate_second_slice(multi_span<int, dynamic_range, dynamic_range, dynamic_range> av)
{
const int expected[6] = {2, 3, 10, 11, 18, 19};
auto section = av.section({0, 1, 0}, {3, 1, 2});
for (auto i = 0; i < section.extent<0>(); ++i) {
for (auto j = 0; j < section.extent<1>(); ++j)
for (auto k = 0; k < section.extent<2>(); ++k) {
auto idx = multi_span_index<3>{i, j, k}; // avoid braces in the CHECK macro
CHECK(section[idx] == expected[2 * i + 2 * j + k]);
}
}
for (auto i = 0; i < section.extent<0>(); ++i) {
for (auto j = 0; j < section.extent<1>(); ++j)
for (auto k = 0; k < section.extent<2>(); ++k)
CHECK(section[i][j][k] == expected[2 * i + 2 * j + k]);
}
int i = 0;
for (const auto num : section) {
CHECK(num == expected[i]);
i++;
}
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
TEST_CASE("strided_span_section_iteration_3d")
TEST(strided_span_tests, strided_span_section_iteration_3d)
{
int arr[3][4][2]{};
for (auto i = 0; i < 3; ++i) {
@@ -707,12 +698,7 @@ TEST_CASE("strided_span_section_iteration_3d")
}
}
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
GSL_SUPPRESS(r.3) // NO-FORMAT: attribute
GSL_SUPPRESS(r.5) // NO-FORMAT: attribute
GSL_SUPPRESS(r.11) // NO-FORMAT: attribute
TEST_CASE("dynamic_strided_span_section_iteration_3d")
TEST(strided_span_tests, dynamic_strided_span_section_iteration_3d)
{
const auto height = 12, width = 2;
const auto size = height * width;
@@ -744,11 +730,13 @@ TEST_CASE("dynamic_strided_span_section_iteration_3d")
delete[] arr;
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute
GSL_SUPPRESS(bounds.2) // NO-FORMAT: attribute
TEST_CASE("strided_span_conversion")
TEST(strided_span_tests, strided_span_conversion)
{
std::set_terminate([] {
std::cerr << "Expected Death. strided_span_conversion";
std::abort();
});
// get an multi_span of 'c' values from the list of X's
struct X
@@ -767,8 +755,8 @@ TEST_CASE("strided_span_conversion")
// convert to 4x12 array of bytes
auto av = as_multi_span(as_bytes(as_multi_span(&arr[0], 4)), dim(d1), dim(d2));
CHECK(av.bounds().index_bounds()[0] == 4);
CHECK(av.bounds().index_bounds()[1] == 12);
EXPECT_TRUE(av.bounds().index_bounds()[0] == 4);
EXPECT_TRUE(av.bounds().index_bounds()[1] == 12);
// get the last 4 columns
auto section = av.section({0, 2 * s}, {4, s}); // { { arr[0].c[0], arr[0].c[1], arr[0].c[2],
@@ -778,8 +766,8 @@ TEST_CASE("strided_span_conversion")
// convert to array 4x1 array of integers
auto cs = section.as_strided_span<int>(); // { { arr[0].c }, {arr[1].c } , ... }
CHECK(cs.bounds().index_bounds()[0] == 4);
CHECK(cs.bounds().index_bounds()[1] == 1);
EXPECT_TRUE(cs.bounds().index_bounds()[0] == 4);
EXPECT_TRUE(cs.bounds().index_bounds()[1] == 1);
// transpose to 1x4 array
strided_bounds<2> reverse_bounds{
@@ -791,16 +779,12 @@ TEST_CASE("strided_span_conversion")
// slice to get a one-dimensional array of c's
strided_span<int, 1> result = transposed[0];
CHECK(result.bounds().index_bounds()[0] == 4);
CHECK_THROWS_AS(result.bounds().index_bounds()[1], fail_fast);
EXPECT_TRUE(result.bounds().index_bounds()[0] == 4);
EXPECT_DEATH(result.bounds().index_bounds()[1], deathstring);
int i = 0;
for (auto& num : result) {
CHECK(num == arr[i].c);
EXPECT_TRUE(num == arr[i].c);
i++;
}
}
#if __clang__ || __GNUC__
#pragma GCC diagnostic pop
#endif
File diff suppressed because it is too large Load Diff
-27
View File
@@ -1,27 +0,0 @@
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
///////////////////////////////////////////////////////////////////////////////
#define CATCH_CONFIG_MAIN
#ifdef _MSC_VER
// blanket turn off warnings from CppCoreCheck from catch
// so people aren't annoyed by them when running the tool.
#include <CodeAnalysis/Warnings.h>
#pragma warning(disable : ALL_CODE_ANALYSIS_WARNINGS) // from catch
#endif // _MSC_VER
#include <catch/catch.hpp>
+45 -42
View File
@@ -14,14 +14,7 @@
//
///////////////////////////////////////////////////////////////////////////////
#ifdef _MSC_VER
// blanket turn off warnings from CppCoreCheck from catch
// so people aren't annoyed by them when running the tool.
#pragma warning(disable : 26440 26426) // from catch
#endif
#include <catch/catch.hpp> // for AssertionHandler, StringRef, CHECK, TEST_...
#include <gtest/gtest.h>
#include <gsl/gsl_util> // for narrow, finally, narrow_cast, narrowing_e...
@@ -30,104 +23,114 @@
#include <limits> // for numeric_limits
#include <stdint.h> // for uint32_t, int32_t
#include <type_traits> // for is_same
#include <cstddef> // for std::ptrdiff_t
using namespace gsl;
TEST_CASE("sanity check for gsl::index typedef")
namespace
{
static constexpr char deathstring[] = "Expected Death";
void f(int& i) { i += 1; }
static int j = 0;
void g() { j += 1; }
}
TEST(utils_tests, sanity_check_for_gsl_index_typedef)
{
static_assert(std::is_same<gsl::index, std::ptrdiff_t>::value,
"gsl::index represents wrong arithmetic type");
}
void f(int& i) { i += 1; }
TEST_CASE("finally_lambda")
TEST(utils_tests, finally_lambda)
{
int i = 0;
{
auto _ = finally([&]() { f(i); });
CHECK(i == 0);
EXPECT_TRUE(i == 0);
}
CHECK(i == 1);
EXPECT_TRUE(i == 1);
}
TEST_CASE("finally_lambda_move")
TEST(utils_tests, finally_lambda_move)
{
int i = 0;
{
auto _1 = finally([&]() { f(i); });
{
auto _2 = std::move(_1);
CHECK(i == 0);
EXPECT_TRUE(i == 0);
}
CHECK(i == 1);
EXPECT_TRUE(i == 1);
{
auto _2 = std::move(_1);
CHECK(i == 1);
EXPECT_TRUE(i == 1);
}
CHECK(i == 1);
EXPECT_TRUE(i == 1);
}
CHECK(i == 1);
EXPECT_TRUE(i == 1);
}
TEST_CASE("finally_function_with_bind")
TEST(utils_tests, finally_function_with_bind)
{
int i = 0;
{
auto _ = finally(std::bind(&f, std::ref(i)));
CHECK(i == 0);
EXPECT_TRUE(i == 0);
}
CHECK(i == 1);
EXPECT_TRUE(i == 1);
}
static int j = 0;
void g() { j += 1; }
TEST_CASE("finally_function_ptr")
TEST(utils_tests, finally_function_ptr)
{
j = 0;
{
auto _ = finally(&g);
CHECK(j == 0);
EXPECT_TRUE(j == 0);
}
CHECK(j == 1);
EXPECT_TRUE(j == 1);
}
GSL_SUPPRESS(con.4) // NO-FORMAT: attribute
TEST_CASE("narrow_cast")
TEST(utils_tests, narrow_cast)
{
int n = 120;
char c = narrow_cast<char>(n);
CHECK(c == 120);
EXPECT_TRUE(c == 120);
n = 300;
unsigned char uc = narrow_cast<unsigned char>(n);
CHECK(uc == 44);
EXPECT_TRUE(uc == 44);
}
GSL_SUPPRESS(con.5) // NO-FORMAT: attribute
TEST_CASE("narrow")
TEST(utils_tests, narrow)
{
std::set_terminate([] {
std::cerr << "Expected Death. narrow";
std::abort();
});
int n = 120;
const char c = narrow<char>(n);
CHECK(c == 120);
EXPECT_TRUE(c == 120);
n = 300;
CHECK_THROWS_AS(narrow<char>(n), narrowing_error);
EXPECT_DEATH(narrow<char>(n), deathstring);
const auto int32_max = std::numeric_limits<int32_t>::max();
const auto int32_min = std::numeric_limits<int32_t>::min();
CHECK(narrow<uint32_t>(int32_t(0)) == 0);
CHECK(narrow<uint32_t>(int32_t(1)) == 1);
CHECK(narrow<uint32_t>(int32_max) == static_cast<uint32_t>(int32_max));
EXPECT_TRUE(narrow<uint32_t>(int32_t(0)) == 0);
EXPECT_TRUE(narrow<uint32_t>(int32_t(1)) == 1);
EXPECT_TRUE(narrow<uint32_t>(int32_max) == static_cast<uint32_t>(int32_max));
CHECK_THROWS_AS(narrow<uint32_t>(int32_t(-1)), narrowing_error);
CHECK_THROWS_AS(narrow<uint32_t>(int32_min), narrowing_error);
EXPECT_DEATH(narrow<uint32_t>(int32_t(-1)), deathstring);
EXPECT_DEATH(narrow<uint32_t>(int32_min), deathstring);
n = -42;
CHECK_THROWS_AS(narrow<unsigned>(n), narrowing_error);
EXPECT_DEATH(narrow<unsigned>(n), deathstring);
#if GSL_CONSTEXPR_NARROW
static_assert(narrow<char>(120) == 120, "Fix GSL_CONSTEXPR_NARROW");
#endif
}