Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / common / test_str_map.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2013 Cloudwatt <libre.licensing@cloudwatt.com>
7  *
8  * Author: Loic Dachary <loic@dachary.org>
9  *
10  *  This library is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU Lesser General Public
12  *  License as published by the Free Software Foundation; either
13  *  version 2.1 of the License, or (at your option) any later version.
14  * 
15  */
16
17 #include <errno.h>
18 #include <gtest/gtest.h>
19
20 #include "include/str_map.h"
21
22 using namespace std;
23
24 TEST(str_map, json) {
25   map<string,string> str_map;
26   stringstream ss;
27   // well formatted
28   ASSERT_EQ(0, get_json_str_map("{\"key\": \"value\"}", ss, &str_map));
29   ASSERT_EQ("value", str_map["key"]);
30   // well formatted but not a JSON object
31   ASSERT_EQ(-EINVAL, get_json_str_map("\"key\"", ss, &str_map));
32   ASSERT_NE(string::npos, ss.str().find("must be a JSON object"));
33 }
34
35 TEST(str_map, plaintext) {
36   {
37     map<string,string> str_map;
38     ASSERT_EQ(0, get_str_map(" foo=bar\t\nfrob=nitz   yeah right=   \n\t",
39                              &str_map));
40     ASSERT_EQ(4u, str_map.size());
41     ASSERT_EQ("bar", str_map["foo"]);
42     ASSERT_EQ("nitz", str_map["frob"]);
43     ASSERT_EQ("", str_map["yeah"]);
44     ASSERT_EQ("", str_map["right"]);
45   }
46   {
47     map<string,string> str_map;
48     ASSERT_EQ(0, get_str_map("that", &str_map));
49     ASSERT_EQ(1u, str_map.size());
50     ASSERT_EQ("", str_map["that"]);
51   }
52   {
53     map<string,string> str_map;
54     ASSERT_EQ(0, get_str_map(" \t \n ", &str_map));
55     ASSERT_EQ(0u, str_map.size());
56     ASSERT_EQ(0, get_str_map("", &str_map));
57     ASSERT_EQ(0u, str_map.size());
58   }
59   {
60     map<string,string> str_map;
61     ASSERT_EQ(0, get_str_map(" key1=val1; key2=\tval2; key3\t = \t val3; \n ", &str_map, "\n;"));
62     ASSERT_EQ(4u, str_map.size());
63     ASSERT_EQ("val1", str_map["key1"]);
64     ASSERT_EQ("val2", str_map["key2"]);
65     ASSERT_EQ("val3", str_map["key3"]);
66   }
67 }
68
69 /* 
70  * Local Variables:
71  * compile-command: "cd ../.. ; make -j4 && 
72  *   make unittest_str_map && 
73  *   valgrind --tool=memcheck --leak-check=full \
74  *      ./unittest_str_map
75  *   "
76  * End:
77  */