Rename runtime/base/zend_* to zend-
[hiphop-php.git] / hphp / test / ext / test_util.cpp
blobf6abd5aacd172cb41c6ea98d8f53497a8e2f5db5
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/test/ext/test_util.h"
18 #include "hphp/util/logger.h"
19 #include "hphp/runtime/base/complex_types.h"
20 #include "hphp/runtime/base/shared_string.h"
21 #include "hphp/runtime/base/zend-string.h"
23 #define VERIFY_DUMP(map, exp) \
24 if (!(exp)) { \
25 printf("%s:%d: [" #exp "] is false\n", __FILE__, __LINE__); \
26 map.dump(); \
27 return Count(false); \
28 } \
30 ///////////////////////////////////////////////////////////////////////////////
32 TestUtil::TestUtil() {
35 ///////////////////////////////////////////////////////////////////////////////
37 bool TestUtil::RunTests(const std::string &which) {
38 bool ret = true;
39 RUN_TEST(TestSharedString);
40 RUN_TEST(TestCanonicalize);
41 RUN_TEST(TestHDF);
42 return ret;
45 ///////////////////////////////////////////////////////////////////////////////
46 // data types
48 struct testhash {
49 size_t operator()(CStrRef s) const {
50 return hash_string(s.data(), s.size());
54 struct testeqstr {
55 bool operator()(CStrRef s1, CStrRef s2) const {
56 return string_strcmp(s1.data(), s1.size(), s2.data(), s2.size()) == 0;
60 struct IHash {
61 size_t operator()(int i) const { return i; }
63 struct IEq {
64 bool operator()(int i, int j) const { return i == j; }
67 bool TestUtil::TestSharedString() {
69 SharedString foo = "foo";
70 SharedString bah("bah");
71 VERIFY(bah->getString() == "bah");
73 SharedString bah2 = bah;
74 VERIFY(bah2->getString() == "bah");
75 VERIFY(bah.get() == bah2.get());
76 SharedString bah3 = "bah";
77 VERIFY(bah.get() == bah3.get());
79 VERIFY(bah->getString() == "bah");
80 VERIFY(foo->getString() == "foo");
83 hphp_shared_string_map<int64_t> map;
84 for (int i = 0; i < 100; i++) {
85 string k("key");
86 k += i;
87 map[k] = i;
89 for (int i = 0; i < 100; i++) {
90 string k("key");
91 k += i;
92 hphp_shared_string_map<int64_t>::const_iterator it = map.find(k);
93 VERIFY(it != map.end());
94 VERIFY(it->second == i);
98 return Count(true);
101 bool TestUtil::TestCanonicalize() {
102 VERIFY(Util::canonicalize("foo") == "foo");
103 VERIFY(Util::canonicalize("/foo") == "/foo");
104 VERIFY(Util::canonicalize("./foo") == "foo");
105 VERIFY(Util::canonicalize("foo/bar") == "foo/bar");
106 VERIFY(Util::canonicalize("foo/////bar") == "foo/bar");
107 VERIFY(Util::canonicalize("foo/bar/") == "foo/bar/");
108 VERIFY(Util::canonicalize("foo/../bar") == "bar");
109 VERIFY(Util::canonicalize("./foo/../bar") == "bar");
110 VERIFY(Util::canonicalize(".////foo/xyz////..////../bar") == "bar");
111 VERIFY(Util::canonicalize("a/foo../bar") == "a/foo../bar");
112 VERIFY(Util::canonicalize("a./foo/./bar") == "a./foo/bar");
113 VERIFY(Util::canonicalize("////a/foo") == "/a/foo");
114 VERIFY(Util::canonicalize("../foo") == "../foo");
115 VERIFY(Util::canonicalize("foo/../../bar") == "../bar");
116 VERIFY(Util::canonicalize("./../../") == "../../");
117 return Count(true);
120 bool TestUtil::TestHDF() {
121 // This was causing a crash
123 Hdf doc, node;
124 node = doc["Node"];
128 Hdf doc;
129 doc.fromString(
130 "node.* {\n"
131 " name = value\n"
132 "}");
133 VS(doc["node"][0]["name"].getString(), "value");
136 return Count(true);