PageRenderTime 29ms CodeModel.GetById 14ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/test/lltimestampcache_tut.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 124 lines | 78 code | 11 blank | 35 comment | 0 complexity | 7ee6a9b5f7f6cfb672972363f6da61ea MD5 | raw file
  1/**
  2 * @file lltimestampcache_tut.cpp
  3 * @author James Tess
  4 * @date 2008-12-03
  5 *
  6 * $LicenseInfo:firstyear=2008&license=viewerlgpl$
  7 * Second Life Viewer Source Code
  8 * Copyright (C) 2010, Linden Research, Inc.
  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;
 13 * version 2.1 of the License only.
 14 * 
 15 * This library is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18 * Lesser General Public License for more details.
 19 * 
 20 * You should have received a copy of the GNU Lesser General Public
 21 * License along with this library; if not, write to the Free Software
 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 23 * 
 24 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 25 * $/LicenseInfo$
 26 */
 27
 28#include <tut/tut.hpp>
 29
 30#include "linden_common.h"
 31#include "../mapserver/lltimestampcache.h"
 32#include "lltut.h"
 33
 34
 35namespace tut
 36{
 37	struct LLTimestampCacheTestData
 38	{
 39	};
 40
 41	typedef test_group<LLTimestampCacheTestData> LLTimestampCacheTestGroup;
 42	typedef LLTimestampCacheTestGroup::object LLTimestampCacheTestObject;
 43
 44	LLTimestampCacheTestGroup timestampCacheTestGroup("LLTimestampCache");
 45
 46	// Most common usage
 47	template<> template<>
 48		void LLTimestampCacheTestObject::test<1>()
 49		{
 50			LLTimestampCache<std::string, std::string> cache;
 51			// put in some data
 52			cache.insert("key1", "val1", 1);
 53			cache.insert("key2", "val2", 2);
 54			cache.insert("key3", "val3", 3);
 55			ensure_equals("size is 3", cache.size(), 3);
 56			// check some items
 57			ensure("has key1", cache.has("key1"));
 58			ensure("no invalid key", !cache.has("invalid key"));
 59			// get some items
 60			ensure_equals("get key1", cache.get("key1", 4), "val1");
 61			ensure_equals("get invalid key", 
 62					cache.get("invalid key", 4), std::string() );
 63			// timestamps
 64			ensure_equals("key1 timestamp updated", cache.getTimestamp("key1"), 4);
 65			ensure_equals("invalid key timestamp", 
 66					cache.getTimestamp("invalid key"), 0);
 67		}
 68
 69	// New empty cache shouldn't have any entries
 70	template<> template<>
 71		void LLTimestampCacheTestObject::test<2>()
 72		{
 73			LLTimestampCache<std::string, std::string> cache;
 74			ensure_equals("starts empty",   cache.size(), 0);
 75			ensure_equals("has nothing",    cache.has("foo"), false);
 76			ensure_equals("gets nothing",   cache.get("foo", 0), std::string() );
 77			U32 max_time = 0xFFFFFFFF;
 78			ensure_equals("erases nothing", cache.eraseBefore(max_time), 0);
 79		}
 80
 81	// Non empty cache
 82	template<> template<>
 83		void LLTimestampCacheTestObject::test<3>()
 84		{
 85			LLTimestampCache<std::string, std::string> cache;
 86			cache.insert("foo", "bar", 123);
 87			ensure_equals("size one", cache.size(), 1);
 88			ensure_equals("has it", cache.has("foo"), true);
 89			ensure_equals("timestamp correct", cache.getTimestamp("foo"), 123);
 90			std::string value = cache.get("foo", 456);
 91			ensure_equals("get value", value, "bar");
 92			ensure_equals("timestamp updated", cache.getTimestamp("foo"), 456);
 93			ensure_equals("erase nothing", cache.eraseBefore(0), 0);
 94			ensure_equals("erase one", cache.eraseBefore(789), 1);
 95			ensure_equals("empty after erase", cache.size(), 0);
 96		}
 97
 98	// Recache of item should update timestamp
 99	template<> template<>
100		void LLTimestampCacheTestObject::test<4>()
101		{
102			LLTimestampCache<std::string, std::string> cache;
103			cache.insert("foo", "bar", 123);
104			cache.insert("foo", "bar", 456);
105			ensure_equals("duplicate suppressed", cache.size(), 1);
106			ensure_equals("timestamp replaced", cache.getTimestamp("foo"), 456);
107		}
108
109	// Erasing some items
110	template<> template<>
111		void LLTimestampCacheTestObject::test<5>()
112		{
113			LLTimestampCache<std::string, std::string> cache;
114			cache.insert("key1", "val1", 1);
115			cache.insert("key2", "val2", 2);
116			cache.insert("key3", "val3", 3);
117			cache.insert("key4", "val4", 4);
118			size_t erased = cache.eraseBefore(3);
119			ensure_equals("erase range", erased, 2);
120			ensure_equals("cache post erase", cache.size(), 2);
121			ensure_equals("has key3", cache.has("key3"), true);
122			ensure_equals("not has key2", cache.has("key2"), false);
123		}
124}