From 6218bb79752ed086945d0099442edc9021ca22a7 Mon Sep 17 00:00:00 2001 From: Ryan Yee Date: Wed, 15 Feb 2017 14:23:02 -0500 Subject: [PATCH] mapAt (#87) * mapAt * Added exception guard * move branch --- .travis.yml | 8 ++--- include/EASTL/map.h | 38 ++++++++++++++++++++ test/packages/EATest/include/EATest/EATest.h | 19 ++++++++++ test/source/TestMap.cpp | 24 ++++++++++++- 4 files changed, 84 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index a21ac343..ecc3b4d5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -50,8 +50,8 @@ install: before_script: - if [[ -n "$USE_CLANG_39" ]]; then export PATH="${CLANG_39_PATH}/bin:$PATH" ;fi - - if [[ -n "$USE_CLANG_39" ]]; then export CC="clang"; fi - - if [[ -n "$USE_CLANG_39" ]]; then export CXX="clang++"; fi + - if [[ -n "$USE_CLANG_39" ]]; then export CC="clang" ;fi + - if [[ -n "$USE_CLANG_39" ]]; then export CXX="clang++" ;fi - if [[ -n "$USE_CLANG_39" ]]; then export CXXFLAGS="$CXXFLAGS -I${CLANG_39_PATH}/include -std=c++11 -stdlib=libc++" ;fi - if [[ -n "$USE_CLANG_39" ]]; then export LDFLAGS="$LDFLAGS -L${CLANG_39_PATH}/lib" ;fi - mkdir build_$EASTL_CONFIG @@ -62,7 +62,7 @@ before_script: script: # Run Tests - cd $TRAVIS_BUILD_DIR/build_$EASTL_CONFIG/test - - ctest -C $EASTL_CONFIG -V + - ctest -C $EASTL_CONFIG -V || exit 1 # Run Benchmarks - cd $TRAVIS_BUILD_DIR/build_$EASTL_CONFIG/benchmark - - ctest -C $EASTL_CONFIG -V + - ctest -C $EASTL_CONFIG -V || exit 1 diff --git a/include/EASTL/map.h b/include/EASTL/map.h index ff399f30..b17fbf4e 100644 --- a/include/EASTL/map.h +++ b/include/EASTL/map.h @@ -155,6 +155,9 @@ namespace eastl T& operator[](Key&& key); #endif + T& at(const Key& key); + const T& at(const Key& key) const; + }; // map @@ -439,6 +442,41 @@ namespace eastl #endif + template + inline T& map::at(const Key& key) + { + iterator itLower(lower_bound(key)); // itLower->first is >= key. + + if(itLower == end()) + { + #if EASTL_EXCEPTIONS_ENABLED + throw std::out_of_range("map::at key does not exist"); + #else + EASTL_FAIL_MSG("map::at key does not exist"); + #endif + } + + return (*itLower).second; + } + + + template + inline const T& map::at(const Key& key) const + { + const_iterator itLower(lower_bound(key)); // itLower->first is >= key. + + if(itLower == end()) + { + #if EASTL_EXCEPTIONS_ENABLED + throw std::out_of_range("map::at key does not exist"); + #else + EASTL_FAIL_MSG("map::at key does not exist"); + #endif + } + + return (*itLower).second; + } + diff --git a/test/packages/EATest/include/EATest/EATest.h b/test/packages/EATest/include/EATest/EATest.h index c36860c1..5033a1de 100644 --- a/test/packages/EATest/include/EATest/EATest.h +++ b/test/packages/EATest/include/EATest/EATest.h @@ -341,6 +341,25 @@ namespace EA #define EATEST_VERIFY_F(bExpression, pFormat, ...) EA::UnitTest::TestInternal::EATEST_VERIFY_F_IMP((bExpression), nErrorCount, __FILE__, __LINE__, pFormat, __VA_ARGS__) #endif + /// EATEST_VERIFY_THROW + /// EATEST_VERIFY_NOTHROW + /// + /// This macro confirms whether or not an expression throws or doesn't throw. If it's not the case that EASTL_EXCEPTIONS_ENABLED + /// the provided expression will not be evaluated. + /// + /// See EATEST_VERIFY for details about error reporting and the _MSG variants + #if EASTL_EXCEPTIONS_ENABLED + #define EATEST_VERIFY_THROW(expression) {bool isNoThrow; try{ {expression;} isNoThrow=true; }catch(...){isNoThrow=false;} EATEST_VERIFY(!isNoThrow); } + #define EATEST_VERIFY_NOTHROW(expression) {bool isNoThrow; try{ {expression;} isNoThrow=true; }catch(...){isNoThrow=false;} EATEST_VERIFY(isNoThrow); } + #define EATEST_VERIFY_THROW_MSG(expression, msg) {bool isNoThrow; try{ {expression;} isNoThrow=true; }catch(...){isNoThrow=false;} EATEST_VERIFY_MSG(!isNoThrow, msg); } + #define EATEST_VERIFY_NOTHROW_MSG(expression, msg) {bool isNoThrow; try{ {expression;} isNoThrow=true; }catch(...){isNoThrow=false;} EATEST_VERIFY_MSG(isNoThrow, msg); } + #else + #define EATEST_VERIFY_THROW(expression) + #define EATEST_VERIFY_NOTHROW(expression) + #define EATEST_VERIFY_THROW_MSG(expression, msg) + #define EATEST_VERIFY_NOTHROW_MSG(expression, msg) + #endif + /////////////////////////////////////////////////////////////////////// /// GetSystemTimeMicroseconds diff --git a/test/source/TestMap.cpp b/test/source/TestMap.cpp index 4f0e9908..824bf67b 100644 --- a/test/source/TestMap.cpp +++ b/test/source/TestMap.cpp @@ -122,7 +122,6 @@ int TestMap() EATEST_VERIFY(m.empty()); } - { // User reports that EASTL_VALIDATE_COMPARE_ENABLED / EASTL_COMPARE_VALIDATE isn't compiling for this case. eastl::map m; @@ -141,6 +140,29 @@ int TestMap() EATEST_VERIFY(v.validate()); } + { + typedef eastl::map IntIntMap; + IntIntMap map1; + + #if EASTL_EXCEPTIONS_ENABLED + EATEST_VERIFY_THROW(map1.at(0)); + #endif + map1[0]=1; + #if EASTL_EXCEPTIONS_ENABLED + EATEST_VERIFY_NOTHROW(map1.at(0)); + #endif + EATEST_VERIFY(map1.at(0) == 1); + + const IntIntMap map2; + const IntIntMap map3(map1); + + #if EASTL_EXCEPTIONS_ENABLED + EATEST_VERIFY_THROW(map2.at(0)); + EATEST_VERIFY_NOTHROW(map3.at(0)); + #endif + EATEST_VERIFY(map3.at(0) == 1); + } + // todo: create a test case for this. // { // // User reports that an incorrectly wrapped pair key used to insert into an eastl map compiles when it should fire a compiler error about unconvertible types.