//============================================================================== // // Copyright (C) 2002 Dick van Oudheusden // // 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., 675 Mass Ave, Cambridge, MA 02139, USA. // //============================================================================== // // $Date: 2006/07/22 13:28:58 $ $Revision: 1.2 $ // //============================================================================== #include #include "ofc/config.h" #include "ofc/DText.h" #include "ofc/DInt.h" #include "ofc/DHashTable.h" #include "DInc.h" #include "DTest.h" //-Collections----------------------------------------------------------------- void DHashTable_test(void) { DHashTable *map = [DHashTable alloc]; DHashTable *copy; DText *key; DText *val; int i; DInt *iKey = [DInt new]; [map init :[DText class]]; STARTTEST(); TEST([map isEmpty]); key = [DText alloc]; [key init :"hello"]; val = [DText alloc]; [val init :"test"]; TEST([map insert :key :val]); key = [DText alloc]; [key init :"bye"]; val = [DText alloc]; [val init :"test"]; TEST([map insert :key :val]); TEST([map length] == 2); key = [DText alloc]; [key init :"see you"]; TEST(![map has :key]); TEST([map delete :key] == nil); [key set :"bye"]; TEST([map has :key]); TEST([map delete :key] != nil); TEST(![map has :key]); [map size :301]; [key set :"hello"]; TEST([map has :key]); TEST([map size] == 301 && [map length] == 1); [map each :@selector(upper)]; val = [map get :key]; TEST(val != nil); TEST([val ccompare :"TEST"] == 0); [map free]; map = [DHashTable alloc]; [map init :[DInt class] :1001 :2.0]; for (i = 0; i < 10000; i++) { DInt *key = [DInt alloc]; DInt *val; [key init :i]; val = [key copy]; [map insert :key :val]; } TEST([map length] == 10000 && [map size] == 8015); { DInt *key = [DInt new]; [key set :0]; TEST([map has :key]); [key set :1234]; TEST([map has :key]); [key set :9999]; TEST([map has :key]); [key set :10000]; TEST(![map has :key]); [key free]; } // Shallow copy copy = [map shallowCopy]; TEST([map length] == [copy length]); for (i = 0; i < 10000; i++) { [iKey set :i]; TEST([map get :iKey] == [copy get :iKey]); } [copy shallowFree]; copy = nil; // (Deep) copy copy = [map copy]; TEST([map length] == [copy length]); for (i = 0; i < 10000; i++) { [iKey set :i]; TEST([map get :iKey] != [copy get :iKey]); TEST([[map get :iKey] get] == [[copy get :iKey] get]); } [copy free]; copy = nil; // End of copy [iKey free]; [map free]; STOPTEST(); }