imgtools/romtools/rombuild/r_t_areaset.cpp
changeset 2 39c28ec933dd
equal deleted inserted replaced
1:820b22e13ff1 2:39c28ec933dd
       
     1 /*
       
     2 * Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * AreaSet Unit Tests
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #if defined(__MSVCDOTNET__) || defined(__TOOLS2__)
       
    21 #include <iostream>
       
    22 #else //!__MSVCDOTNET__
       
    23 #include <iostream.h>
       
    24 #endif //__MSVCDOTNET__
       
    25 
       
    26 #include <stdlib.h>
       
    27 #include <string.h>
       
    28 #include <stdarg.h>
       
    29 
       
    30 #include "r_areaset.h"
       
    31 #include "r_rom.h"
       
    32 
       
    33 ////////////////////////////////////////////////////////////////////////
       
    34 
       
    35 LOCAL_C void Test(TBool aExpr, const char* aMsg)
       
    36 	{
       
    37 	if (! aExpr)
       
    38 		{
       
    39 		cerr << "Test Failed: " << aMsg << '\n';
       
    40 		exit(1);
       
    41 		}
       
    42 	}
       
    43 
       
    44 
       
    45 LOCAL_C void CheckAreas(const AreaSet* aPAreaSet, ...)
       
    46 	{
       
    47 	va_list l;
       
    48 	va_start(l, aPAreaSet);
       
    49 
       
    50 	TInt areaCount;
       
    51 	for (areaCount = 0;; ++areaCount)
       
    52 		{
       
    53 		const char* name = va_arg(l, const char*);
       
    54 		if (name == 0)
       
    55 			break;
       
    56 
       
    57 		TLinAddr startAddr = va_arg(l, TLinAddr);
       
    58 		TUint size = va_arg(l, TUint);
       
    59 
       
    60 		const Area* pArea = aPAreaSet->FindByName(name);
       
    61 		Test(pArea != 0, "unknown name");
       
    62 		Test(pArea->DestBaseAddr() == startAddr, "incorrect area start address");
       
    63 		Test(pArea->MaxSize() == size, "incorrect area size");
       
    64 		}
       
    65 
       
    66 	Test(areaCount == aPAreaSet->Count(), "incorrect number of areas");
       
    67 
       
    68 	va_end(l);
       
    69 	}
       
    70 
       
    71 ////////////////////////////////////////////////////////////////////////
       
    72 
       
    73 LOCAL_C void TestAddAreaSuccess()
       
    74 	{
       
    75 	cout << "TestAddAreaSuccess...\n";
       
    76 
       
    77 	AreaSet areaSet;
       
    78 
       
    79 	const char KName1[] = "toto";
       
    80 	const TLinAddr KStart1 = 0x666;
       
    81 	const TUint KSize1 = 0x42;
       
    82 
       
    83 	const char* overlappingArea;
       
    84 	AreaSet::TAddResult r = areaSet.AddArea(KName1, KStart1, KSize1, overlappingArea);
       
    85 	Test(r == AreaSet::EAdded, "adding area 1");
       
    86 	Test(overlappingArea == 0, "incorrect overlapping area 1");
       
    87 	CheckAreas(&areaSet, KName1, KStart1, KSize1, 0);
       
    88 
       
    89 	const char KName2[] = "foobar";
       
    90 	const TLinAddr KStart2 = 0x100000;
       
    91 	const TUint KSize2 = 0x100;
       
    92 
       
    93 	r = areaSet.AddArea(KName2, KStart2, KSize2, overlappingArea);
       
    94 	Test(r == AreaSet::EAdded, "adding area 2");
       
    95 	Test(overlappingArea == 0, "incorrect overlapping area 2");
       
    96 	CheckAreas(&areaSet, KName1, KStart1, KSize1, KName2, KStart2, KSize2, 0);
       
    97 	}
       
    98 
       
    99 
       
   100 LOCAL_C void TestAddingTwoAreas(const char* aName1, TLinAddr aDestBaseAddr1, TUint aSize1,
       
   101 								const char* aName2, TLinAddr aDestBaseAddr2, TUint aSize2,
       
   102 								AreaSet::TAddResult aExpectedResult)
       
   103 	{
       
   104 	cout << "Testing overlap between " << aName1 << " and " << aName2 << "\n";
       
   105 
       
   106 	AreaSet areaSet;
       
   107 
       
   108 	const char* overlappingArea;
       
   109 	AreaSet::TAddResult r = areaSet.AddArea(aName1, aDestBaseAddr1, aSize1, overlappingArea);
       
   110 	Test(r == AreaSet::EAdded, "adding area 1");
       
   111 	Test(overlappingArea == 0, "incorrect overlapping area 1");
       
   112 
       
   113 	r = areaSet.AddArea(aName2, aDestBaseAddr2, aSize2, overlappingArea);
       
   114 	Test(r == aExpectedResult, "adding area 2");
       
   115 
       
   116 	Test(areaSet.Count() == ((aExpectedResult == AreaSet::EAdded) ? 2 : 1),
       
   117 		 "incorrect area count");
       
   118 	if (aExpectedResult == AreaSet::EAdded)
       
   119 		{
       
   120 		Test(areaSet.Count() == 2, "incorrect area count (should be 2)");
       
   121 		Test(overlappingArea == 0, "incorrect overlapping area 2 (should be 0)");
       
   122 		}
       
   123 	else
       
   124 		{
       
   125 		Test(areaSet.Count() == 1, "incorrect area count (should be 1)");
       
   126 		if (aExpectedResult == AreaSet::EOverlap)
       
   127 			Test(strcmp(overlappingArea, aName1) == 0, "incorrect overlapping area 2 (bad name)");
       
   128 		else
       
   129 			Test(overlappingArea == 0, "incorrect overlapping area 2 (should be 0)");
       
   130 		}
       
   131 	}
       
   132 
       
   133 
       
   134 LOCAL_C void TestAddAreaOverlap()
       
   135 	{
       
   136 	cout << "TestAddAreaOverlap...\n";
       
   137 
       
   138 	const char KNameInitial[] = "initial";
       
   139 	const TLinAddr KStartInitial = 0x1000;
       
   140 	const TUint KSizeInitial = 0x101;
       
   141 
       
   142 	// new area overlapping first byte of initial one
       
   143 	TestAddingTwoAreas(KNameInitial, KStartInitial, KSizeInitial,
       
   144 					   "overlap 1", 0x0F00, 0x101, AreaSet::EOverlap);
       
   145 
       
   146 	// new area overlapping last byte of initial one
       
   147 	TestAddingTwoAreas(KNameInitial, KStartInitial, KSizeInitial,
       
   148 					   "overlap 2", 0x01100, 0x101, AreaSet::EOverlap);
       
   149 
       
   150 	// new area embedded in the initial one
       
   151 	TestAddingTwoAreas(KNameInitial, KStartInitial, KSizeInitial,
       
   152 					   "overlap 3", 0x01010, 0x10, AreaSet::EOverlap);
       
   153 
       
   154 	// existing area overlapping first byte of new one
       
   155 	TestAddingTwoAreas(KNameInitial, 0x0F00, 0x101, "overlap 10",
       
   156 					   KStartInitial, KSizeInitial, AreaSet::EOverlap);
       
   157 
       
   158 	// existing area overlapping last byte of new one
       
   159 	TestAddingTwoAreas(KNameInitial, 0x01100, 0x101, "overlap 11",
       
   160 					   KStartInitial, KSizeInitial, AreaSet::EOverlap);
       
   161 
       
   162 	// existing area embedded in the new one
       
   163 	TestAddingTwoAreas(KNameInitial, 0x01010, 0x10, "overlap 12",
       
   164 					   KStartInitial, KSizeInitial, AreaSet::EOverlap);
       
   165 
       
   166 	// new area just before the initial one
       
   167 	TestAddingTwoAreas(KNameInitial, KStartInitial, KSizeInitial,
       
   168 					   "overlap 4", 0x0F00, 0x100, AreaSet::EAdded);
       
   169 	
       
   170 	// new area just after the initial one
       
   171 	TestAddingTwoAreas(KNameInitial, KStartInitial, KSizeInitial,
       
   172 					   "overlap 5", 0x01101, 0x100, AreaSet::EAdded);
       
   173 	}
       
   174 
       
   175 
       
   176 LOCAL_C void TestAddAreaDuplicateName()
       
   177 	{
       
   178 	cout << "TestAddAreaDuplicateName...\n";
       
   179 	
       
   180 	TestAddingTwoAreas("foobar", 0x10, 0x10,
       
   181 					   "foobar", 0x100, 0x10,
       
   182 					   AreaSet::EDuplicateName);
       
   183 	}
       
   184 
       
   185 
       
   186 LOCAL_C void TestAddAreaOverflow()
       
   187 	{
       
   188 	cout << "TestAddAreaOverflow...\n";
       
   189 	
       
   190 	AreaSet areaSet;
       
   191 
       
   192 	const char KName1[] = "foobar";
       
   193 	const char* overlappingArea;
       
   194 	AreaSet::TAddResult r = areaSet.AddArea(KName1, 0xFFFFFFFF, 0x02, overlappingArea);
       
   195 	Test(r == AreaSet::EOverflow, "adding area 1");
       
   196 	Test(areaSet.Count() == 0, "incorrect count after trying to add area 1");
       
   197 	Test(areaSet.FindByName(KName1) == 0, "Unexpected name found after trying to add area 1");
       
   198 	Test(overlappingArea == 0, "incorrect overlapping area 1");
       
   199 
       
   200 	const char KName2[] = "barfoo";
       
   201 	r = areaSet.AddArea(KName2, 0xFFFFFFFF, 0xFFFFFFFF, overlappingArea);
       
   202 	Test(r == AreaSet::EOverflow, "adding area 2");
       
   203 	Test(areaSet.Count() == 0, "incorrect count after trying to add area 2");
       
   204 	Test(areaSet.FindByName(KName2) == 0, "Unexpected name found after trying to add area 2");
       
   205 	Test(overlappingArea == 0, "incorrect overlapping area 2");
       
   206 	}
       
   207 
       
   208 
       
   209 
       
   210 LOCAL_C void TestAddArea()
       
   211 	{
       
   212 	TestAddAreaSuccess();
       
   213 	TestAddAreaOverlap();
       
   214 	TestAddAreaDuplicateName();
       
   215 	TestAddAreaOverflow();
       
   216 	}
       
   217 
       
   218 LOCAL_C void TestSrcAddrManipulations()
       
   219 	{
       
   220 	cout << "TestSrcAddrManipulations...\n";
       
   221 
       
   222 	//
       
   223 	// Creating an AreaSet instance containing one area
       
   224 	//
       
   225 
       
   226 	AreaSet areaSet;
       
   227 	const char* overlappingArea;
       
   228 	const char KAreaName[] = "foobar";
       
   229 	const TUint KMaxSize = 10;
       
   230 	AreaSet::TAddResult r = areaSet.AddArea(KAreaName, 0x100, KMaxSize, overlappingArea);
       
   231 	Test(r == AreaSet::EAdded, "Failed to add area");
       
   232 
       
   233 	Area* area = areaSet.FindByName(KAreaName);
       
   234 	Test(area != 0, "Failed to find area");
       
   235 
       
   236 	Test(area->UsedSize() == 0, "used size before allocation");
       
   237 
       
   238 	const TUint KSrcBaseAddr = 0x100;
       
   239 	area->SetSrcBaseAddr(KSrcBaseAddr);
       
   240 
       
   241 	Test(area->SrcBaseAddr() == KSrcBaseAddr, "destination base address before allocation");
       
   242 	Test(area->SrcBaseAddr() == area->SrcLimitAddr(), "destination limit address before allocation");
       
   243 
       
   244 	//
       
   245 	// Allocating some space in the area
       
   246 	//
       
   247 
       
   248 	const TUint KAlloc1 = KMaxSize-1;
       
   249 	TUint overflow;
       
   250 	TBool allocated = area->ExtendSrcLimitAddr(KSrcBaseAddr+KAlloc1, overflow);
       
   251 	Test(allocated, "allocation 1 failed");
       
   252 	Test(area->UsedSize() == KAlloc1, "used size after allocation 1");
       
   253 	Test(area->SrcBaseAddr()+KAlloc1 == area->SrcLimitAddr(), "destination limit address after allocation 1");
       
   254 
       
   255 	//
       
   256 	// Allocating more than available
       
   257 	//
       
   258 
       
   259 	const TUint KAlloc2 = KMaxSize*2;
       
   260 	allocated = area->ExtendSrcLimitAddr(KSrcBaseAddr+KAlloc1+KAlloc2, overflow);
       
   261 	Test(! allocated, "allocation 2 should have failed");
       
   262 	Test(overflow == KAlloc2+KAlloc1 - KMaxSize, "overflow after allocation 2");
       
   263 	Test(area->UsedSize() == KAlloc1, "used size after allocation 2");
       
   264 	Test(area->SrcBaseAddr()+KAlloc1 == area->SrcLimitAddr(), "destination limit address after allocation 2");
       
   265 
       
   266 	//
       
   267 	// Allocating just enough to fill the area completely  
       
   268 	//
       
   269 
       
   270 	const TUint KAlloc3 = KMaxSize-KAlloc1;
       
   271 	allocated = area->ExtendSrcLimitAddr(KSrcBaseAddr+KAlloc1+KAlloc3, overflow);
       
   272 	Test(allocated, "allocation 3 failed");
       
   273 	Test(area->UsedSize() == KAlloc1+KAlloc3, "used size after allocation 3");
       
   274 	Test(area->UsedSize() == area->MaxSize(), "used size and max size should be equal");
       
   275 	Test(area->SrcBaseAddr()+KAlloc1+KAlloc3 == area->SrcLimitAddr(), "destination limit address after allocation 3");
       
   276 
       
   277 	//
       
   278 	// Overflowing the area by one byte
       
   279 	//
       
   280 
       
   281 	const TUint KAlloc4 = 1;
       
   282 	allocated = area->ExtendSrcLimitAddr(KSrcBaseAddr+KAlloc1+KAlloc3+KAlloc4, overflow);
       
   283 	Test(! allocated, "allocation 4 should have failed");
       
   284 	Test(overflow == 1, "overflow after allocation 4");
       
   285 	Test(area->UsedSize() == KAlloc1+KAlloc3, "used size after allocation 4");
       
   286 	Test(area->SrcBaseAddr()+KAlloc1+KAlloc3 == area->SrcLimitAddr(), "destination limit address after allocation 4");
       
   287 	}
       
   288 
       
   289 
       
   290 LOCAL_C void TestFileIterator()
       
   291 	{
       
   292 	cout << "TestFileIterator...\n";
       
   293 
       
   294 	//
       
   295 	// Creating an area set containing one area
       
   296 	//
       
   297 
       
   298 	AreaSet areaSet;
       
   299 	const char* overlappingArea;
       
   300 	const char KAreaName[] = "foobar";
       
   301 	const TUint KMaxSize = 10;
       
   302 	AreaSet::TAddResult r = areaSet.AddArea(KAreaName, 0x100, KMaxSize, overlappingArea);
       
   303 	Test(r == AreaSet::EAdded, "Failed to add area");
       
   304 
       
   305 	Area* area = areaSet.FindByName(KAreaName);
       
   306 	Test(area != 0, "Failed to find area");
       
   307 
       
   308 	FilesInAreaIterator it1(*area);
       
   309 	Test(it1.IsDone(), "it1.IsDone()");
       
   310 
       
   311 	//
       
   312 	// Adding one file to that area
       
   313 	//
       
   314 	
       
   315 	TRomBuilderEntry* pfile1 = new TRomBuilderEntry("file1", (TText*) "file1");
       
   316 	area->AddFile(pfile1);
       
   317 
       
   318 	FilesInAreaIterator it2(*area);
       
   319 	Test(! it2.IsDone(), "! it2.IsDone() 1");
       
   320 	Test(it2.Current() == pfile1, "it2.Current() == pfile1");
       
   321 
       
   322 	it2.GoToNext();
       
   323 	Test(it2.IsDone(), "it2.IsDone()");
       
   324 
       
   325 	//
       
   326 	// Adding a second file to that area
       
   327 	//
       
   328 
       
   329 	TRomBuilderEntry* pFile2 = new TRomBuilderEntry("file2", (TText*) "file2");
       
   330 	area->AddFile(pFile2);
       
   331 
       
   332 	FilesInAreaIterator it3(*area);
       
   333 	Test(! it3.IsDone(), "! it3.IsDone() 1");
       
   334 	Test(it3.Current() == pfile1, "it3.Current() == pfile1");
       
   335 
       
   336 	it3.GoToNext();
       
   337 	Test(! it3.IsDone(), "it3.IsDone() 2");
       
   338 	Test(it3.Current() == pFile2, "it3.Current() == pFile2");
       
   339 
       
   340 	it3.GoToNext();
       
   341 	Test(it3.IsDone(), "it3.IsDone()");
       
   342 	}
       
   343 
       
   344 
       
   345 LOCAL_C void TestNonDefaultAreaIterator() 
       
   346 	{
       
   347 	cout << "TestNonDefaultAreaIterator...\n";
       
   348 
       
   349 	//
       
   350 	// Creating an area set
       
   351 	//
       
   352 
       
   353 	AreaSet areaSet;
       
   354 
       
   355 	NonDefaultAreasIterator it1(areaSet);
       
   356 	Test(it1.IsDone(), "it1.IsDone()");
       
   357 	
       
   358 	//
       
   359 	// Adding a first non default area
       
   360 	//
       
   361 
       
   362 	const char* overlappingArea;
       
   363 	const char KAreaName1[] = "area 1";
       
   364 	AreaSet::TAddResult r = areaSet.AddArea(KAreaName1, 0x100, 0x10, overlappingArea);
       
   365 	Test(r == AreaSet::EAdded, "Failed to add area 1");
       
   366 
       
   367 	Area* pArea1 = areaSet.FindByName(KAreaName1);
       
   368 	Test(pArea1 != 0, "Failed to find area 1");
       
   369 
       
   370 	NonDefaultAreasIterator it2(areaSet);
       
   371 	Test(! it2.IsDone(), "! it2.IsDone()");
       
   372 
       
   373 	Test(&it2.Current() == pArea1, "&it2.Current() == pArea1");
       
   374 
       
   375 	it2.GoToNext();
       
   376 	Test(it2.IsDone(), "it2.IsDone()");
       
   377 
       
   378 	//
       
   379 	// Adding a default area
       
   380 	//
       
   381 
       
   382 	r = areaSet.AddArea(AreaSet::KDefaultAreaName, 0x50000000, 0x00200000, overlappingArea);
       
   383 	Test(r == AreaSet::EAdded, "failed to add default area");
       
   384 
       
   385 	NonDefaultAreasIterator it3(areaSet);
       
   386 	Test(! it3.IsDone(), "! it3.IsDone()");
       
   387 
       
   388 	Test(&it3.Current() == pArea1, "&it3.Current() == pArea1");
       
   389 
       
   390 	it3.GoToNext();
       
   391 	Test(it3.IsDone(), "it3.IsDone()");
       
   392 
       
   393 	//
       
   394 	// Adding a second non default area
       
   395 	//
       
   396 
       
   397 	const char KAreaName2[] = "area 2";
       
   398 	r = areaSet.AddArea(KAreaName2, 0x1000, 0x10, overlappingArea);
       
   399 	Test(r == AreaSet::EAdded, "Failed to add area 2");
       
   400 
       
   401 	Area* pArea2 = areaSet.FindByName(KAreaName2);
       
   402 	Test(pArea2 != 0, "Failed to find area 2");
       
   403 
       
   404 	NonDefaultAreasIterator it4(areaSet);
       
   405 	Test(! it4.IsDone(), "! it4.IsDone()");
       
   406 
       
   407 	Test(&it4.Current() == pArea2, "&it4.Current() == pArea2");
       
   408 
       
   409 	it4.GoToNext();
       
   410 	Test(! it4.IsDone(), "it4.IsDone()");
       
   411 	Test(&it4.Current() == pArea1, "&it4.Current() == pArea1");
       
   412 
       
   413 	it4.GoToNext();
       
   414 	Test(it4.IsDone(), "it4.IsDone()");
       
   415 	}
       
   416 
       
   417 ////////////////////////////////////////////////////////////////////////
       
   418 
       
   419 GLDEF_C int main() 
       
   420 	{
       
   421 	TestAddArea();
       
   422 	TestSrcAddrManipulations();
       
   423 	TestFileIterator();
       
   424 	TestNonDefaultAreaIterator();
       
   425 
       
   426 	cout << "\nTests OK\n";
       
   427 	return 0;
       
   428 	}