tests/test-hashmap.cpp
changeset 13 ef6b69693bab
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tests/test-hashmap.cpp	Tue Aug 27 00:55:44 2013 -0700
     1.3 @@ -0,0 +1,112 @@
     1.4 +// vim: set sts=8 ts=2 sw=2 tw=99 et:
     1.5 +//
     1.6 +// Copyright (C) 2013, David Anderson and AlliedModders LLC
     1.7 +// All rights reserved.
     1.8 +// 
     1.9 +// Redistribution and use in source and binary forms, with or without
    1.10 +// modification, are permitted provided that the following conditions are met:
    1.11 +// 
    1.12 +//  * Redistributions of source code must retain the above copyright notice, this
    1.13 +//    list of conditions and the following disclaimer.
    1.14 +//  * Redistributions in binary form must reproduce the above copyright notice,
    1.15 +//    this list of conditions and the following disclaimer in the documentation
    1.16 +//    and/or other materials provided with the distribution.
    1.17 +//  * Neither the name of AlliedModders LLC nor the names of its contributors
    1.18 +//    may be used to endorse or promote products derived from this software
    1.19 +//    without specific prior written permission.
    1.20 +//
    1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.22 +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.23 +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.24 +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.25 +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.26 +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.27 +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.28 +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.29 +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.30 +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.31 +// POSSIBILITY OF SUCH DAMAGE.
    1.32 +
    1.33 +#include <am-hashmap.h>
    1.34 +#include <am-utility.h>
    1.35 +#include <am-string.h>
    1.36 +#include "runner.h"
    1.37 +
    1.38 +using namespace ke;
    1.39 +
    1.40 +struct StringPolicy
    1.41 +{
    1.42 +  static inline uint32_t hash(const char *key) {
    1.43 +    return FastHashCharSequence(key, strlen(key));
    1.44 +  }
    1.45 +  static inline bool matches(const char *find, const AString &key) {
    1.46 +    return key.compare(find) == 0;
    1.47 +  }
    1.48 +};
    1.49 +
    1.50 +class TestHashMap : public Test
    1.51 +{
    1.52 + public:
    1.53 +  TestHashMap()
    1.54 +   : Test("HashMap")
    1.55 +  {
    1.56 +  }
    1.57 +
    1.58 +  bool testBasics() {
    1.59 +    typedef HashMap<AString, int, StringPolicy> Map;
    1.60 +    Map map;
    1.61 +
    1.62 +    if (!check(map.init(), "initialized"))
    1.63 +      return false;
    1.64 +
    1.65 +    Map::Result r = map.find("cat");
    1.66 +    if (!check(!r.found(), "cat not found"))
    1.67 +      return false;
    1.68 +
    1.69 +    Map::Insert i = map.findForAdd("cat");
    1.70 +    if (!check(!i.found(), "cat not found for add"))
    1.71 +      return false;
    1.72 +    if (!check(map.add(i, AString("cat"), 5), "cat added"))
    1.73 +      return false;
    1.74 +    if (!check(r->value == 5, "cat is 5"))
    1.75 +      return false;
    1.76 +
    1.77 +    Map::iterator iter = map.iter();
    1.78 +    if (!check(iter->key.compare("cat") == 0, "iterator got key cat"))
    1.79 +      return false;
    1.80 +    if (!check(iter->value == 5, "iterator got value 5"))
    1.81 +      return false;
    1.82 +    iter.next();
    1.83 +    if (!check(iter.empty(), "iterator should be finished"))
    1.84 +      return false;
    1.85 +
    1.86 +    i = map.findForAdd("cat");
    1.87 +    if (!check(i.found(), "cat found through insert"))
    1.88 +      return false;
    1.89 +
    1.90 +    r = map.find("dog");
    1.91 +    if (!check(!r.found(), "dog not found"))
    1.92 +      return false;
    1.93 +
    1.94 +    r = map.find("cat");
    1.95 +    if (!check(r.found(), "cat found"))
    1.96 +      return false;
    1.97 +    if (!check(r->value == 5, "cat is 5"))
    1.98 +      return false;
    1.99 +    map.remove(r);
   1.100 +
   1.101 +    r = map.find("cat");
   1.102 +    if (!check(!r.found(), "cat ran away"))
   1.103 +      return false;
   1.104 +
   1.105 +    return true;
   1.106 +  }
   1.107 +
   1.108 +  bool Run() KE_OVERRIDE
   1.109 +  {
   1.110 +    if (!testBasics())
   1.111 +      return false;
   1.112 +    return true;
   1.113 +  }
   1.114 +} sTestHashMap;
   1.115 +