Apply code review changes

This commit is contained in:
Vincent Cogne 2016-06-09 11:17:39 +02:00
parent 90281cfdc3
commit 12f1164bfc
2 changed files with 31 additions and 21 deletions

View File

@ -7,12 +7,12 @@
#pragma once #pragma once
#endif #endif
#include <array>
#include <limits> #include <limits>
#include <list> #include <list>
#include <map> #include <map>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include <array>
#include "yaml-cpp/binary.h" #include "yaml-cpp/binary.h"
#include "yaml-cpp/node/impl.h" #include "yaml-cpp/node/impl.h"
@ -243,29 +243,29 @@ struct convert<std::list<T> > {
}; };
// std::array // std::array
template <typename T, std::size_t S> template <typename T, std::size_t N>
struct convert<std::array<T, S> > { struct convert<std::array<T, N>> {
static Node encode(const std::array<T, S>& rhs) { static Node encode(const std::array<T, N>& rhs) {
Node node(NodeType::Sequence); Node node(NodeType::Sequence);
for (typename std::array<T, S>::const_iterator it = rhs.begin(); for (auto element : rhs) {
it != rhs.end(); ++it) node.push_back(element);
node.push_back(*it); }
return node; return node;
} }
static bool decode(const Node& node, std::array<T, S>& rhs) { static bool decode(const Node& node, std::array<T, N>& rhs) {
if (!node.IsSequence()) if (!node.IsSequence())
return false; return false;
if (node.size() != N)
return false;
std::size_t index = 0; for (auto i = 0u; i < node.size(); ++i) {
for (const_iterator it = node.begin(); it != node.end(); ++it) {
#if defined(__GNUC__) && __GNUC__ < 4 #if defined(__GNUC__) && __GNUC__ < 4
// workaround for GCC 3: // workaround for GCC 3:
rhs.at(index) = it->template as<T>(); rhs[i] = node[i].template as<T>();
#else #else
rhs.at(index) = it->as<T>(); rhs[i] = node[i].as<T>();
#endif #endif
++index;
} }
return true; return true;
} }

View File

@ -12,6 +12,14 @@
using ::testing::AnyOf; using ::testing::AnyOf;
using ::testing::Eq; using ::testing::Eq;
#define EXPECT_THROW_REPRESENTATION_EXCEPTION(statement, message) \
ASSERT_THROW(statement, RepresentationException); \
try { \
statement; \
} catch (const RepresentationException& e) { \
EXPECT_EQ(e.msg, message); \
}
namespace YAML { namespace YAML {
namespace { namespace {
TEST(NodeTest, SimpleScalar) { TEST(NodeTest, SimpleScalar) {
@ -155,19 +163,21 @@ TEST(NodeTest, SimpleSubkeys) {
} }
TEST(NodeTest, StdArray) { TEST(NodeTest, StdArray) {
std::array<int, 5> evens; std::array<int, 5> evens {{ 2, 4, 6, 8, 10 }};
evens[0] = 2;
evens[1] = 4;
evens[2] = 6;
evens[3] = 8;
evens[4] = 10;
Node node; Node node;
node["evens"] = evens; node["evens"] = evens;
std::array<int, 5> actualEvens = node["evens"].as<std::array<int, 5> >(); std::array<int, 5> actualEvens = node["evens"].as<std::array<int, 5>>();
EXPECT_EQ(evens, actualEvens); EXPECT_EQ(evens, actualEvens);
} }
TEST(NodeTest, StdArrayWrongSize) {
std::array<int, 3> evens {{ 2, 4, 6 }};
Node node;
node["evens"] = evens;
EXPECT_THROW_REPRESENTATION_EXCEPTION((node["evens"].as<std::array<int, 5>>()),
ErrorMsg::BAD_CONVERSION);
}
TEST(NodeTest, StdVector) { TEST(NodeTest, StdVector) {
std::vector<int> primes; std::vector<int> primes;
primes.push_back(2); primes.push_back(2);