PageRenderTime 24ms CodeModel.GetById 8ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/test/lltut.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 205 lines | 149 code | 27 blank | 29 comment | 14 complexity | bea6f26ccc522f7bf50ec6eb2dcbb23b MD5 | raw file
  1/** 
  2 * @file lltut.cpp
  3 * @author Mark Lentczner
  4 * @date 5/16/06
  5 * @brief MacTester
  6 *
  7 * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  8 * Second Life Viewer Source Code
  9 * Copyright (C) 2010, Linden Research, Inc.
 10 * 
 11 * This library is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU Lesser General Public
 13 * License as published by the Free Software Foundation;
 14 * version 2.1 of the License only.
 15 * 
 16 * This library is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 19 * Lesser General Public License for more details.
 20 * 
 21 * You should have received a copy of the GNU Lesser General Public
 22 * License along with this library; if not, write to the Free Software
 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 24 * 
 25 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 26 * $/LicenseInfo$
 27 */
 28
 29#include "linden_common.h"
 30
 31#include "lltut.h"
 32
 33#include "lldate.h"
 34#include "llformat.h"
 35#include "llsd.h"
 36#include "lluri.h"
 37#include "stringize.h"
 38
 39namespace tut
 40{
 41	void ensure_equals(const char* msg, const LLDate& actual,
 42		const LLDate& expected)
 43	{
 44		ensure_equals(msg,
 45			actual.secondsSinceEpoch(), expected.secondsSinceEpoch());
 46	}
 47
 48	void ensure_equals(const char* msg, const LLURI& actual,
 49		const LLURI& expected)
 50	{
 51		ensure_equals(msg,
 52			actual.asString(), expected.asString());
 53	}
 54
 55	void ensure_equals(const char* msg,
 56		const std::vector<U8>& actual, const std::vector<U8>& expected)
 57	{
 58		std::string s(msg);
 59		
 60		ensure_equals(s + " size", actual.size(), expected.size());
 61		
 62		std::vector<U8>::const_iterator i, j;
 63		int k;
 64		for (i = actual.begin(), j = expected.begin(), k = 0;
 65			i != actual.end();
 66			++i, ++j, ++k)
 67		{
 68			ensure_equals(s + " field", *i, *j);
 69		}
 70	}
 71
 72	void ensure_equals(const char* m, const LLSD& actual,
 73		const LLSD& expected)
 74    {
 75        ensure_equals(std::string(m), actual, expected);
 76    }
 77
 78	void ensure_equals(const std::string& msg, const LLSD& actual,
 79		const LLSD& expected)
 80	{
 81		ensure_equals(msg + " type", actual.type(), expected.type());
 82		switch (actual.type())
 83		{
 84			case LLSD::TypeUndefined:
 85				return;
 86			
 87			case LLSD::TypeBoolean:
 88				ensure_equals(msg + " boolean", actual.asBoolean(), expected.asBoolean());
 89				return;
 90			
 91			case LLSD::TypeInteger:
 92				ensure_equals(msg + " integer", actual.asInteger(), expected.asInteger());
 93				return;
 94			
 95			case LLSD::TypeReal:
 96				ensure_equals(msg + " real", actual.asReal(), expected.asReal());
 97				return;
 98			
 99			case LLSD::TypeString:
100				ensure_equals(msg + " string", actual.asString(), expected.asString());
101				return;
102			
103			case LLSD::TypeUUID:
104				ensure_equals(msg + " uuid", actual.asUUID(), expected.asUUID());
105				return;
106			
107			case LLSD::TypeDate:
108				ensure_equals(msg + " date", actual.asDate(), expected.asDate());
109				return;
110				
111			case LLSD::TypeURI:
112				ensure_equals(msg + " uri", actual.asURI(), expected.asURI());
113				return;
114		
115			case LLSD::TypeBinary:
116				ensure_equals(msg + " binary", actual.asBinary(), expected.asBinary());
117				return;
118		
119			case LLSD::TypeMap:
120			{
121				ensure_equals(msg + " map size", actual.size(), expected.size());
122				
123				LLSD::map_const_iterator actual_iter = actual.beginMap();
124				LLSD::map_const_iterator expected_iter = expected.beginMap();
125				
126				while(actual_iter != actual.endMap())
127				{
128					ensure_equals(msg + " map keys", 
129						actual_iter->first, expected_iter->first);
130					ensure_equals(msg + "[" + actual_iter->first + "]",
131						actual_iter->second, expected_iter->second);
132					++actual_iter;
133					++expected_iter;
134				}
135				return;
136			}
137			case LLSD::TypeArray:
138			{
139				ensure_equals(msg + " array size", actual.size(), expected.size());
140				
141				for(int i = 0; i < actual.size(); ++i)
142				{
143					ensure_equals(msg + llformat("[%d]", i),
144						actual[i], expected[i]);
145				}
146				return;
147			}
148			default:
149				// should never get here, but compiler produces warning if we
150				// don't cover this case, and at Linden warnings are fatal.
151				throw failure(STRINGIZE("invalid type field " << actual.type()));
152		}
153	}
154
155	void ensure_starts_with(const std::string& msg,
156		const std::string& actual, const std::string& expectedStart)
157	{
158		if( actual.find(expectedStart, 0) != 0 )
159		{
160			std::stringstream ss;
161			ss << msg << ": " << "expected to find " << expectedStart
162				<< " at start of actual " << actual;
163			throw failure(ss.str().c_str());
164		}
165	}
166
167	void ensure_ends_with(const std::string& msg,
168		const std::string& actual, const std::string& expectedEnd)
169	{
170		if( actual.size() < expectedEnd.size()
171			|| actual.rfind(expectedEnd)
172				!= (actual.size() - expectedEnd.size()) )
173		{
174			std::stringstream ss;
175			ss << msg << ": " << "expected to find " << expectedEnd
176				<< " at end of actual " << actual;
177			throw failure(ss.str().c_str());
178		}
179	}
180
181	void ensure_contains(const std::string& msg,
182		const std::string& actual, const std::string& expectedSubString)
183	{
184		if( actual.find(expectedSubString, 0) == std::string::npos )
185		{
186			std::stringstream ss;
187			ss << msg << ": " << "expected to find " << expectedSubString
188				<< " in actual " << actual;
189			throw failure(ss.str().c_str());
190		}
191	}
192
193	void ensure_does_not_contain(const std::string& msg,
194		const std::string& actual, const std::string& expectedSubString)
195	{
196		if( actual.find(expectedSubString, 0) != std::string::npos )
197		{
198			std::stringstream ss;
199			ss << msg << ": " << "expected not to find " << expectedSubString
200				<< " in actual " << actual;
201			throw failure(ss.str().c_str());
202		}
203	}
204}
205