llinstancetracker_test.cpp
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:7k
源码类别:

游戏引擎

开发平台:

C++ Builder

  1. /**
  2.  * @file   llinstancetracker_test.cpp
  3.  * @author Nat Goodspeed
  4.  * @date   2009-11-10
  5.  * @brief  Test for llinstancetracker.
  6.  * 
  7.  * $LicenseInfo:firstyear=2009&license=viewergpl$
  8.  * 
  9.  * Copyright (c) 2009-2010, Linden Research, Inc.
  10.  * 
  11.  * Second Life Viewer Source Code
  12.  * The source code in this file ("Source Code") is provided by Linden Lab
  13.  * to you under the terms of the GNU General Public License, version 2.0
  14.  * ("GPL"), unless you have obtained a separate licensing agreement
  15.  * ("Other License"), formally executed by you and Linden Lab.  Terms of
  16.  * the GPL can be found in doc/GPL-license.txt in this distribution, or
  17.  * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  18.  * 
  19.  * There are special exceptions to the terms and conditions of the GPL as
  20.  * it is applied to this Source Code. View the full text of the exception
  21.  * in the file doc/FLOSS-exception.txt in this software distribution, or
  22.  * online at
  23.  * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  24.  * 
  25.  * By copying, modifying or distributing this software, you acknowledge
  26.  * that you have read and understood your obligations described above,
  27.  * and agree to abide by those obligations.
  28.  * 
  29.  * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  30.  * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  31.  * COMPLETENESS OR PERFORMANCE.
  32.  * $/LicenseInfo$
  33.  */
  34. // Precompiled header
  35. #include "linden_common.h"
  36. // associated header
  37. #include "llinstancetracker.h"
  38. // STL headers
  39. #include <string>
  40. #include <vector>
  41. #include <set>
  42. #include <algorithm>                // std::sort()
  43. // std headers
  44. // external library headers
  45. #include <boost/scoped_ptr.hpp>
  46. // other Linden headers
  47. #include "../test/lltut.h"
  48. struct Keyed: public LLInstanceTracker<Keyed, std::string>
  49. {
  50.     Keyed(const std::string& name):
  51.         LLInstanceTracker<Keyed, std::string>(name),
  52.         mName(name)
  53.     {}
  54.     std::string mName;
  55. };
  56. struct Unkeyed: public LLInstanceTracker<Unkeyed>
  57. {
  58. };
  59. /*****************************************************************************
  60. *   TUT
  61. *****************************************************************************/
  62. namespace tut
  63. {
  64.     struct llinstancetracker_data
  65.     {
  66.     };
  67.     typedef test_group<llinstancetracker_data> llinstancetracker_group;
  68.     typedef llinstancetracker_group::object object;
  69.     llinstancetracker_group llinstancetrackergrp("llinstancetracker");
  70.     template<> template<>
  71.     void object::test<1>()
  72.     {
  73.         ensure_equals(Keyed::instanceCount(), 0);
  74.         {
  75.             Keyed one("one");
  76.             ensure_equals(Keyed::instanceCount(), 1);
  77.             Keyed* found = Keyed::getInstance("one");
  78.             ensure("couldn't find stack Keyed", found);
  79.             ensure_equals("found wrong Keyed instance", found, &one);
  80.             {
  81.                 boost::scoped_ptr<Keyed> two(new Keyed("two"));
  82.                 ensure_equals(Keyed::instanceCount(), 2);
  83.                 Keyed* found = Keyed::getInstance("two");
  84.                 ensure("couldn't find heap Keyed", found);
  85.                 ensure_equals("found wrong Keyed instance", found, two.get());
  86.             }
  87.             ensure_equals(Keyed::instanceCount(), 1);
  88.         }
  89.         Keyed* found = Keyed::getInstance("one");
  90.         ensure("Keyed key lives too long", ! found);
  91.         ensure_equals(Keyed::instanceCount(), 0);
  92.     }
  93.     template<> template<>
  94.     void object::test<2>()
  95.     {
  96.         ensure_equals(Unkeyed::instanceCount(), 0);
  97.         {
  98.             Unkeyed one;
  99.             ensure_equals(Unkeyed::instanceCount(), 1);
  100.             Unkeyed* found = Unkeyed::getInstance(&one);
  101.             ensure_equals(found, &one);
  102.             {
  103.                 boost::scoped_ptr<Unkeyed> two(new Unkeyed);
  104.                 ensure_equals(Unkeyed::instanceCount(), 2);
  105.                 Unkeyed* found = Unkeyed::getInstance(two.get());
  106.                 ensure_equals(found, two.get());
  107.             }
  108.             ensure_equals(Unkeyed::instanceCount(), 1);
  109.         }
  110.         ensure_equals(Unkeyed::instanceCount(), 0);
  111.     }
  112.     template<> template<>
  113.     void object::test<3>()
  114.     {
  115.         Keyed one("one"), two("two"), three("three");
  116.         // We don't want to rely on the underlying container delivering keys
  117.         // in any particular order. That allows us the flexibility to
  118.         // reimplement LLInstanceTracker using, say, a hash map instead of a
  119.         // std::map. We DO insist that every key appear exactly once.
  120.         typedef std::vector<std::string> StringVector;
  121.         StringVector keys(Keyed::beginKeys(), Keyed::endKeys());
  122.         std::sort(keys.begin(), keys.end());
  123.         StringVector::const_iterator ki(keys.begin());
  124.         ensure_equals(*ki++, "one");
  125.         ensure_equals(*ki++, "three");
  126.         ensure_equals(*ki++, "two");
  127.         // Use ensure() here because ensure_equals would want to display
  128.         // mismatched values, and frankly that wouldn't help much.
  129.         ensure("didn't reach end", ki == keys.end());
  130.         // Use a somewhat different approach to order independence with
  131.         // beginInstances(): explicitly capture the instances we know in a
  132.         // set, and delete them as we iterate through.
  133.         typedef std::set<Keyed*> InstanceSet;
  134.         InstanceSet instances;
  135.         instances.insert(&one);
  136.         instances.insert(&two);
  137.         instances.insert(&three);
  138.         for (Keyed::instance_iter ii(Keyed::beginInstances()), iend(Keyed::endInstances());
  139.              ii != iend; ++ii)
  140.         {
  141.             Keyed& ref = *ii;
  142.             ensure_equals("spurious instance", instances.erase(&ref), 1);
  143.         }
  144.         ensure_equals("unreported instance", instances.size(), 0);
  145.     }
  146.     template<> template<>
  147.     void object::test<4>()
  148.     {
  149.         Unkeyed one, two, three;
  150.         typedef std::set<Unkeyed*> KeySet;
  151.         KeySet keys;
  152.         keys.insert(&one);
  153.         keys.insert(&two);
  154.         keys.insert(&three);
  155. {
  156. Unkeyed::LLInstanceTrackerScopedGuard guard;
  157. for (Unkeyed::key_iter ki(guard.beginKeys()), kend(guard.endKeys());
  158.      ki != kend; ++ki)
  159. {
  160. ensure_equals("spurious key", keys.erase(*ki), 1);
  161. }
  162. }
  163.         ensure_equals("unreported key", keys.size(), 0);
  164.         KeySet instances;
  165.         instances.insert(&one);
  166.         instances.insert(&two);
  167.         instances.insert(&three);
  168. {
  169. Unkeyed::LLInstanceTrackerScopedGuard guard;
  170. for (Unkeyed::instance_iter ii(guard.beginInstances()), iend(guard.endInstances());
  171.      ii != iend; ++ii)
  172. {
  173. Unkeyed& ref = *ii;
  174. ensure_equals("spurious instance", instances.erase(&ref), 1);
  175. }
  176. }
  177.         ensure_equals("unreported instance", instances.size(), 0);
  178.     }
  179. } // namespace tut