Skip to content

Commit

Permalink
Add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
maierlars committed Jun 16, 2023
1 parent 7640a9e commit 46e574a
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 16 deletions.
52 changes: 40 additions & 12 deletions include/velocypack/String.h
Expand Up @@ -30,32 +30,50 @@ namespace arangodb::velocypack {

template<typename Allocator = std::allocator<uint8_t>>
struct BasicString : SliceBase<BasicString<Allocator>, Slice> {
using allocator_type = Allocator;
using allocator_type = Allocator;

explicit BasicString(uint8_t const* start)
: _mem(start, Slice(start).byteSize()) {}
BasicString(Slice s)
: _mem(s.getDataPtr(), s.byteSize()) {}
BasicString() = default;
BasicString(BasicString const&) = default;
BasicString(BasicString&&) noexcept = default;

explicit BasicString(uint8_t const* start)
: _str(start, Slice(start).byteSize()) {}
BasicString(Slice s) : _str(s.getDataPtr(), s.byteSize()) {}

// allocator aware constructors
BasicString(BasicString const& o, allocator_type alloc)
: _str(o._str, alloc) {}
BasicString(BasicString&& o, allocator_type alloc)
: _str(std::move(o._str), alloc) {}

explicit BasicString(uint8_t const* start, allocator_type a)
: _str(start, Slice(start).byteSize(), a) {}
BasicString(Slice s, allocator_type a)
: _str(s.getDataPtr(), s.byteSize(), a) {}

BasicString& operator=(BasicString const&) = default;
BasicString& operator=(BasicString&&) noexcept = default;

BasicString& operator=(Slice s) {
_mem.assign(s.getDataPtr(), s.byteSize());
_str.assign(s.getDataPtr(), s.byteSize());
return *this;
}
BasicString& operator=(uint8_t const* s) { return *this = Slice(s); }
BasicString() = default;

uint8_t const* getDataPtr() const noexcept { return _mem.c_str(); }
uint8_t const* getDataPtr() const noexcept { return _str.c_str(); }

// similar to std::string we decay into Slice
Slice slice() { return Slice{getDataPtr()}; }
operator Slice() { return slice(); }

Slice slice() { return Slice{getDataPtr()}; }
void set(uint8_t const* s) { *this = Slice(s); }

using StringType =
std::basic_string<uint8_t, std::char_traits<uint8_t>, allocator_type>;
StringType const& getUnderlyingString() const noexcept { return _str; }

private:
std::basic_string<uint8_t, std::char_traits<uint8_t>, Allocator> _mem;
StringType _str;
};

using String = BasicString<>;
Expand All @@ -64,12 +82,12 @@ namespace pmr {
using String = BasicString<std::pmr::polymorphic_allocator<uint8_t>>;
}

using VPackString = arangodb::velocypack::String;

extern template struct SliceBase<String, Slice>;
extern template struct SliceBase<pmr::String, Slice>;
} // namespace arangodb::velocypack

using VPackString = arangodb::velocypack::String;

namespace std {
// implementation of std::hash for a String object
template<>
Expand All @@ -82,4 +100,14 @@ struct hash<arangodb::velocypack::String> {
#endif
}
};
template<>
struct hash<arangodb::velocypack::pmr::String> {
std::size_t operator()(arangodb::velocypack::pmr::String const& slice) const {
#ifdef VELOCYPACK_32BIT
return static_cast<size_t>(slice.hash32());
#else
return static_cast<std::size_t>(slice.hash());
#endif
}
};
} // namespace std
2 changes: 2 additions & 0 deletions src/Slice.cpp
Expand Up @@ -25,6 +25,8 @@
#include <velocypack/Slice.h>
#include "SliceBase.tpp"

using namespace arangodb::velocypack;

uint8_t const Slice::noneSliceData[] = {0x00};
uint8_t const Slice::illegalSliceData[] = {0x17};
uint8_t const Slice::nullSliceData[] = {0x18};
Expand Down
6 changes: 3 additions & 3 deletions src/SliceBase.tpp
Expand Up @@ -35,8 +35,6 @@
#include "velocypack/Slice.h"
#include "velocypack/ValueType.h"

using namespace arangodb::velocypack;

namespace {

// maximum values for integers of different byte sizes
Expand All @@ -46,6 +44,8 @@ constexpr int64_t maxValues[] = {

} // namespace

namespace arangodb::velocypack {

// translates an integer key into a string
template<typename DerivedType, typename SliceType>
SliceType SliceBase<DerivedType, SliceType>::translate() const {
Expand Down Expand Up @@ -948,7 +948,7 @@ ValueLength SliceBase<DerivedType, SliceType>::length() const {
return readIntegerNonEmpty<ValueLength>(start() + end - offsetSize,
offsetSize);
}

} // namespace arangodb::velocypack
#define INSTANTIATE_TYPE(Derived, SliceType) \
template struct SliceBase<Derived, SliceType>; \
template SliceType SliceBase<Derived, SliceType>::searchObjectKeyBinary<1>( \
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Expand Up @@ -32,6 +32,7 @@ set(Tests
testsSink
testsSlice
testsSliceContainer
testsString
testsType
testsValidator
testsVersion
Expand Down
3 changes: 2 additions & 1 deletion tests/testsSlice.cpp
Expand Up @@ -33,7 +33,8 @@

#include <velocypack/String.h>

using SliceTypes = testing::Types<VPackSlice, VPackString>;
using SliceTypes =
testing::Types<VPackSlice, VPackString, arangodb::velocypack::pmr::String>;
template<class>
struct SliceTest : testing::Test {};
TYPED_TEST_SUITE(SliceTest, SliceTypes);
Expand Down
52 changes: 52 additions & 0 deletions tests/testsString.cpp
@@ -0,0 +1,52 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief Library to build up VPack documents.
///
/// DISCLAIMER
///
/// Copyright 2023 ArangoDB GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Lars Maier
////////////////////////////////////////////////////////////////////////////////

#include <vector>
#include <memory_resource>

#include "tests-common.h"

#include <velocypack/String.h>

using namespace arangodb::velocypack;

TEST(StringTest, pmr_container) {
std::pmr::vector<pmr::String> vec{std::pmr::new_delete_resource()};
vec.emplace_back(VPackSlice::emptyArraySlice());
vec.emplace_back(VPackSlice::emptyArraySlice().getDataPtr());

auto other = pmr::String {VPackSlice::emptyObjectSlice()};
vec.emplace_back(other);
vec.emplace_back(std::move(other));

for (auto const& e : vec) {
EXPECT_EQ(e.getUnderlyingString().get_allocator(), vec.get_allocator());
}
}

int main(int argc, char* argv[]) {
::testing::InitGoogleTest(&argc, argv);

return RUN_ALL_TESTS();
}

0 comments on commit 46e574a

Please sign in to comment.