/*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
// @author Nicholas Ormrod <njormrod@fb.com>
-#include "folly/DynamicConverter.h"
-#include <gtest/gtest.h>
+#include <folly/DynamicConverter.h>
+
+#include <algorithm>
#include <gflags/gflags.h>
-#include "folly/Benchmark.h"
+#include <gtest/gtest.h>
+#include <map>
+#include <vector>
+
+#include <folly/Benchmark.h>
using namespace folly;
using namespace folly::dynamicconverter_detail;
bool c1f = is_container<int>::value;
bool c2f = is_container<std::pair<int, int>>::value;
bool c3f = is_container<A>::value;
+ bool c4f = class_is_container<A>::value;
bool c1t = is_container<std::vector<int>>::value;
bool c2t = is_container<std::set<int>>::value;
bool c3t = is_container<std::map<int, int>>::value;
+ bool c4t = class_is_container<std::vector<A>>::value;
EXPECT_EQ(c1f, false);
EXPECT_EQ(c2f, false);
EXPECT_EQ(c3f, false);
+ EXPECT_EQ(c4f, false);
EXPECT_EQ(c1t, true);
EXPECT_EQ(c2t, true);
EXPECT_EQ(c3t, true);
+ EXPECT_EQ(c4t, true);
+
- bool m1f = is_map_container<int>::value;
- bool m2f = is_map_container<std::vector<int>>::value;
- bool m3f = is_map_container<std::set<int>>::value;
+ bool m1f = is_map<int>::value;
+ bool m2f = is_map<std::set<int>>::value;
- bool m1t = is_map_container<std::map<int, int>>::value;
- bool m2t = is_map_container<std::unordered_map<int, int>>::value;
+ bool m1t = is_map<std::map<int, int>>::value;
EXPECT_EQ(m1f, false);
EXPECT_EQ(m2f, false);
- EXPECT_EQ(m3f, false);
EXPECT_EQ(m1t, true);
- EXPECT_EQ(m2t, true);
+
+
+ bool r1f = is_range<int>::value;
+
+ bool r1t = is_range<std::set<int>>::value;
+ bool r2t = is_range<std::vector<int>>::value;
+
+ EXPECT_EQ(r1f, false);
+ EXPECT_EQ(r1t, true);
+ EXPECT_EQ(r2t, true);
}
TEST(DynamicConverter, arithmetic_types) {
auto i2 = convertTo<int64_t>(d2);
EXPECT_EQ(i2, 123456789012345);
- dynamic d3 = 123456789012345;
- auto i3 = convertTo<uint8_t>(d3);
- EXPECT_EQ(i3, 121);
-
dynamic d4 = 3.141;
auto i4 = convertTo<float>(d4);
EXPECT_EQ((int)(i4*100), 314);
EXPECT_EQ(i2, i2b);
}
+TEST(DynamicConverter, map_keyed_by_string) {
+ dynamic d1 = dynamic::object("1", "one")("2", "two");
+ auto i1 = convertTo<std::map<std::string, std::string>>(d1);
+ decltype(i1) i1b = { { "1", "one" }, { "2", "two" } };
+ EXPECT_EQ(i1, i1b);
+
+ dynamic d2 = { { "3", "three" }, { "4", "four" } };
+ auto i2 = convertTo<std::unordered_map<std::string, std::string>>(d2);
+ decltype(i2) i2b = { { "3", "three" }, { "4", "four" } };
+ EXPECT_EQ(i2, i2b);
+}
+
+TEST(DynamicConverter, map_to_vector_of_pairs) {
+ dynamic d1 = dynamic::object("1", "one")("2", "two");
+ auto i1 = convertTo<std::vector<std::pair<std::string, std::string>>>(d1);
+ std::sort(i1.begin(), i1.end());
+ decltype(i1) i1b = { { "1", "one" }, { "2", "two" } };
+ EXPECT_EQ(i1, i1b);
+}
+
TEST(DynamicConverter, nested_containers) {
dynamic d1 = { { 1 }, { }, { 2, 3 } };
auto i1 = convertTo<folly::fbvector<std::vector<uint8_t>>>(d1);
dynamic
dv1 = {},
dv2 = { ds1, ds2 },
- dv3 = { ds3 };
+ dv3({ ds3 });
dynamic
dm1 = dynamic::object(true, dv1)(false, dv2),
EXPECT_EQ(f1, i);
}
+TEST(DynamicConverter, consts) {
+ dynamic d1 = 7.5;
+ auto i1 = convertTo<const double>(d1);
+ EXPECT_EQ(7.5, i1);
+
+ dynamic d2 = "Hello";
+ auto i2 = convertTo<const std::string>(d2);
+ decltype(i2) i2b = "Hello";
+ EXPECT_EQ(i2b, i2);
+
+ dynamic d3 = true;
+ auto i3 = convertTo<const bool>(d3);
+ EXPECT_EQ(true, i3);
+
+ dynamic d4 = "true";
+ auto i4 = convertTo<const bool>(d4);
+ EXPECT_EQ(true, i4);
+
+ dynamic d5 = { 1, 2 };
+ auto i5 = convertTo<const std::pair<const int, const int>>(d5);
+ decltype(i5) i5b = { 1, 2 };
+ EXPECT_EQ(i5b, i5);
+}
+
struct Token {
int kind_;
fbstring lexeme_;
explicit Token(int kind, const fbstring& lexeme)
: kind_(kind), lexeme_(lexeme) {}
};
+
namespace folly {
template <> struct DynamicConverter<Token> {
static Token convert(const dynamic& d) {
}
};
}
+
TEST(DynamicConverter, example) {
dynamic d1 = dynamic::object("KIND", 2)("LEXEME", "a token");
auto i1 = convertTo<Token>(d1);
EXPECT_EQ(i1.lexeme_, "a token");
}
+TEST(DynamicConverter, construct) {
+ using std::vector;
+ using std::map;
+ using std::pair;
+ using std::string;
+ {
+ vector<int> c { 1, 2, 3 };
+ dynamic d = { 1, 2, 3 };
+ EXPECT_EQ(d, toDynamic(c));
+ }
+
+ {
+ map<int, int> c { { 2, 4 }, { 3, 9 } };
+ dynamic d = dynamic::object(2, 4)(3, 9);
+ EXPECT_EQ(d, toDynamic(c));
+ }
+
+ {
+ map<string, string> c { { "a", "b" } };
+ dynamic d = dynamic::object("a", "b");
+ EXPECT_EQ(d, toDynamic(c));
+ }
+
+ {
+ map<string, pair<string, int>> c { { "a", { "b", 3 } } };
+ dynamic d = dynamic::object("a", dynamic { "b", 3 });
+ EXPECT_EQ(d, toDynamic(c));
+ }
+
+ {
+ map<string, pair<string, int>> c { { "a", { "b", 3 } } };
+ dynamic d = dynamic::object("a", dynamic { "b", 3 });
+ EXPECT_EQ(d, toDynamic(c));
+ }
+
+ {
+ vector<int> vi { 2, 3, 4, 5 };
+ auto c = std::make_pair(range(vi.begin(), vi.begin() + 3),
+ range(vi.begin() + 1, vi.begin() + 4));
+ dynamic d = { { 2, 3, 4 }, { 3, 4, 5 } };
+ EXPECT_EQ(d, toDynamic(c));
+ }
+}
+
+TEST(DynamicConverter, errors) {
+ const auto int32Over =
+ static_cast<int64_t>(std::numeric_limits<int32_t>().max()) + 1;
+ const auto floatOver =
+ static_cast<double>(std::numeric_limits<float>().max()) * 2;
+
+ dynamic d1 = int32Over;
+ EXPECT_THROW(convertTo<int32_t>(d1), std::range_error);
+
+ dynamic d2 = floatOver;
+ EXPECT_THROW(convertTo<float>(d2), std::range_error);
+}
+
int main(int argc, char ** argv) {
testing::InitGoogleTest(&argc, argv);
- google::ParseCommandLineFlags(&argc, &argv, true);
+ gflags::ParseCommandLineFlags(&argc, &argv, true);
if (FLAGS_benchmark) {
folly::runBenchmarks();
}
return RUN_ALL_TESTS();
}
-