From: <sv...@ww...> - 2004-06-28 03:31:14
|
Author: mkrose Date: 2004-06-27 20:31:08 -0700 (Sun, 27 Jun 2004) New Revision: 1089 Added: trunk/CSP/SimData/SimData/Tests/test_Ref.cpp Modified: trunk/CSP/SimData/CHANGES.current Log: Added a unit test for reference counting. Browse at: https://www.zerobar.net/viewcvs/viewcvs.cgi?view=rev&rev=1089 Modified: trunk/CSP/SimData/CHANGES.current =================================================================== --- trunk/CSP/SimData/CHANGES.current 2004-06-28 01:34:48 UTC (rev 1088) +++ trunk/CSP/SimData/CHANGES.current 2004-06-28 03:31:08 UTC (rev 1089) @@ -13,6 +13,8 @@ * Small fixes to TaggedRecord.h + * Added a unit test for reference counting. + 2004-06-26: onsight * Remove superflous logging in interface registry, and suppress swig warnings. Added: trunk/CSP/SimData/SimData/Tests/test_Ref.cpp =================================================================== --- trunk/CSP/SimData/SimData/Tests/test_Ref.cpp 2004-06-28 01:34:48 UTC (rev 1088) +++ trunk/CSP/SimData/SimData/Tests/test_Ref.cpp 2004-06-28 03:31:08 UTC (rev 1089) @@ -0,0 +1,70 @@ +/* SimData: Data Infrastructure for Simulations + * Copyright (C) 2004 Mark Rose <mk...@us...> + * + * This file is part of SimData. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + + +/** + * @file test_Ref.h + * @brief Test reference counting. + */ + + +#include <SimData/Ref.h> + +#include <cstdlib> +#include <cassert> + +int count = 0; + +struct R: public simdata::Referenced { + R() { ++count; } + ~R() { --count; } +}; + +typedef simdata::Ref<R> Ref; + +void test() { + { + assert(count == 0); + Ref x = new R; + assert(count == 1); + { + Ref y = x; + assert(count == 1); + } + assert(count == 1); + { + Ref y = new R; + assert(count == 2); + y = x; + assert(count == 1); + y = new R; + assert(count == 2); + } + assert(count == 1); + } + assert(count == 0); + ::exit(0); +} + +int main() { + test(); + return 0; +}; + |