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

View File

@ -12,6 +12,14 @@
using ::testing::AnyOf;
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 {
TEST(NodeTest, SimpleScalar) {
@ -155,19 +163,21 @@ TEST(NodeTest, SimpleSubkeys) {
}
TEST(NodeTest, StdArray) {
std::array<int, 5> evens;
evens[0] = 2;
evens[1] = 4;
evens[2] = 6;
evens[3] = 8;
evens[4] = 10;
std::array<int, 5> evens {{ 2, 4, 6, 8, 10 }};
Node node;
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);
}
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) {
std::vector<int> primes;
primes.push_back(2);