X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fceph%2Fsrc%2Ftest%2Fcommon%2Ftest_str_map.cc;fp=src%2Fceph%2Fsrc%2Ftest%2Fcommon%2Ftest_str_map.cc;h=e96c792a4780e39e51dcc444f3b61c3a19e03efe;hb=812ff6ca9fcd3e629e49d4328905f33eee8ca3f5;hp=0000000000000000000000000000000000000000;hpb=15280273faafb77777eab341909a3f495cf248d9;p=stor4nfv.git diff --git a/src/ceph/src/test/common/test_str_map.cc b/src/ceph/src/test/common/test_str_map.cc new file mode 100644 index 0000000..e96c792 --- /dev/null +++ b/src/ceph/src/test/common/test_str_map.cc @@ -0,0 +1,77 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2013 Cloudwatt + * + * Author: Loic Dachary + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + */ + +#include +#include + +#include "include/str_map.h" + +using namespace std; + +TEST(str_map, json) { + map str_map; + stringstream ss; + // well formatted + ASSERT_EQ(0, get_json_str_map("{\"key\": \"value\"}", ss, &str_map)); + ASSERT_EQ("value", str_map["key"]); + // well formatted but not a JSON object + ASSERT_EQ(-EINVAL, get_json_str_map("\"key\"", ss, &str_map)); + ASSERT_NE(string::npos, ss.str().find("must be a JSON object")); +} + +TEST(str_map, plaintext) { + { + map str_map; + ASSERT_EQ(0, get_str_map(" foo=bar\t\nfrob=nitz yeah right= \n\t", + &str_map)); + ASSERT_EQ(4u, str_map.size()); + ASSERT_EQ("bar", str_map["foo"]); + ASSERT_EQ("nitz", str_map["frob"]); + ASSERT_EQ("", str_map["yeah"]); + ASSERT_EQ("", str_map["right"]); + } + { + map str_map; + ASSERT_EQ(0, get_str_map("that", &str_map)); + ASSERT_EQ(1u, str_map.size()); + ASSERT_EQ("", str_map["that"]); + } + { + map str_map; + ASSERT_EQ(0, get_str_map(" \t \n ", &str_map)); + ASSERT_EQ(0u, str_map.size()); + ASSERT_EQ(0, get_str_map("", &str_map)); + ASSERT_EQ(0u, str_map.size()); + } + { + map str_map; + ASSERT_EQ(0, get_str_map(" key1=val1; key2=\tval2; key3\t = \t val3; \n ", &str_map, "\n;")); + ASSERT_EQ(4u, str_map.size()); + ASSERT_EQ("val1", str_map["key1"]); + ASSERT_EQ("val2", str_map["key2"]); + ASSERT_EQ("val3", str_map["key3"]); + } +} + +/* + * Local Variables: + * compile-command: "cd ../.. ; make -j4 && + * make unittest_str_map && + * valgrind --tool=memcheck --leak-check=full \ + * ./unittest_str_map + * " + * End: + */