home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 April: Mac OS SDK / Dev.CD Apr 97 SDK1.toast / Development Kits (Disc 1) / Apple Shared Library Manager / ASLM Examples / ExampleLibrary / Sources / LibraryManagerTest.cp < prev    next >
Encoding:
Text File  |  1996-11-19  |  6.8 KB  |  289 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        LibraryManagerTest.cp
  3.  
  4.     Contains:    MPW Tool to test out the ASLM.
  5.  
  6.     Description:
  7.         
  8.         LibraryManagerTest1 [-v] [-t 0|1] [-c nReps]
  9.         
  10.         -v             turns verbose mode on (progress messages in MPW window), it is off by default
  11.         -t          turns tracing on (tracing to the Trace Monitor), it is off by default
  12.         -c nReps    nReps must be a positive integer for number of times through the test while loop
  13.         -s             shutdown the ASLM
  14.  
  15.         This test program can be modified to test any class you develop and do simple stress
  16.         testing by changing what is inside the test while loop.
  17.         
  18.     Caveats:
  19.     
  20.         Command-period will leave some things dangling that will never be disposed.
  21.         
  22.  
  23.     Copyright:    © 1991-1994 by Apple Computer, Inc., all rights reserved.
  24.  
  25. */
  26.  
  27. #ifndef __STDDEF__
  28. #include <stddef.h>
  29. #endif
  30. #ifndef __QUICKDRAW__
  31. #include <QuickDraw.h>
  32. #endif
  33. #ifndef __CTYPE__
  34. #include <ctype.h>
  35. #endif
  36. #ifndef __IOSTREAM__
  37. #include <iostream.h>
  38. #endif
  39. #ifndef __STDIO__
  40. #include <stdio.h>
  41. #endif
  42. #ifndef __EVENTS__
  43. #include <Events.h>
  44. #endif
  45.  
  46. #ifndef __EXAMPLECLASS__
  47. #include "ExampleClass.h"
  48. #endif
  49. #ifndef __LIBRARYMANAGERUTILITIES__
  50. #include <LibraryManagerUtilities.h>
  51. #endif
  52.  
  53. static int getIntArgument(char *arg);
  54. extern "C" void DoTests(TExampleClass* object1, TExampleClass* object2, int count, Boolean trace, Boolean verbose, char* testClassInfoClass);
  55.  
  56. const kTestPoolSize = 0;    // set to 0 to test autogrow pools
  57. const ulong kTestHelloCount = 100000;
  58.  
  59. ulong helloticks = 0;
  60.  
  61. void testDynamicFunctions();
  62. char* localHello(ulong& theHelloTicks);
  63.  
  64. void printUsage(char* progName)
  65. {
  66.     fprintf(stderr, "# Usage - %s [-v] [-c numReps] [-t] [-s] [-l] [-i <classid>]\n", progName);
  67.     fprintf(stderr, "-v verbose, -c repetitions, -t trace on, -s shutdown ASLM -l load ASLM -i <GetClassInfo test classid>\n");
  68. }
  69.  
  70. main(int argc, char *argv[]) {
  71.  
  72.     int paramNdx;
  73.     int theCount = 1;
  74.     Boolean verbose = false;
  75.     Boolean trace = false;
  76.     Boolean debugging = false;
  77.     char* testClassInfoClass = 0;
  78.  
  79.     for (paramNdx = 1; paramNdx < argc; paramNdx++) {
  80.  
  81.         if (tolower(*(argv[paramNdx])) == '-') {  // found an option
  82.  
  83.             switch (tolower(*(argv[paramNdx]+1))) {
  84.             
  85.                 case 'c': 
  86.                     
  87.                     if (++paramNdx < argc) {
  88.                         theCount = getIntArgument(argv[paramNdx]);
  89.                         if (theCount == -1) {
  90.                             fprintf(stderr, 
  91.                                 "### option -c value is not a positive integer: %s\n", 
  92.                                 argv[paramNdx]);
  93.                             return 1;
  94.                         }
  95.                     }
  96.                     else {
  97.                         fprintf(stderr, "### Not enough arguments\n");
  98.                         return 1;
  99.                     }
  100.                     break;
  101.                 
  102.                 case 'l':
  103.                     
  104.                     LoadLibraryManager();
  105.                     return 0;
  106.                 
  107.                 case 'v':
  108.                 
  109.                     verbose = true;
  110.                     break;
  111.                 
  112.                 case 't':
  113.                 
  114.                     trace = true;
  115.                     break;
  116.                     
  117.                 case 'x':
  118.                     
  119.                     debugging = true;
  120.                     break;
  121.                     
  122.                 case 's':
  123.                 
  124.                     UnloadLibraryManager();
  125.                     if (paramNdx+1 < argc && tolower(*(argv[paramNdx+1]+1)) == 'l')
  126.                         LoadLibraryManager();
  127.                     return 0;
  128.                     
  129.                 case 'i':
  130.                     
  131.                     if (++paramNdx < argc) {
  132.                         testClassInfoClass = argv[paramNdx];
  133.                     }
  134.                     else {
  135.                         fprintf(stderr, "### Not enough arguments\n");
  136.                         printUsage(argv[0]);
  137.                     }
  138.                     
  139.                     break;
  140.                         
  141.  
  142.                 default:
  143.             
  144.                     fprintf(stderr,"### %s - \"%s\" is not an option.\n", 
  145.                             argv[0], argv[paramNdx]);
  146.                     printUsage(argv[0]);
  147.                     return 1;
  148.             }
  149.         }
  150.     }
  151.     
  152.     
  153.     if (debugging) 
  154.         DebugStr("\p LibraryManagerTest");
  155.     
  156.     // Now, on with business
  157.     if (verbose) cout << "Entering LibraryManagerTest" << endl; 
  158.  
  159.     if (verbose) cout << "Calling InitLibraryManager" << endl; 
  160.     
  161.     // Call InitLibraryManager
  162.     InitLibraryManager(kTestPoolSize, kSystemZone);
  163.     TLibraryManager* testMgr = GetLocalLibraryManager();
  164.     if (testMgr == NULL) {
  165.         cout << "Bad News: testMgr is NULL" << endl;
  166.         return 1;
  167.     }
  168.  
  169.     if (LoadLibraries() != kNoError)
  170.     {
  171.         cout << "Bad News: Couldn't preload libraries" << endl;
  172.         CleanupLibraryManager();
  173.         return 1;
  174.     }
  175.     
  176.     // turn trace on or off according to -t option
  177.     if (!trace)
  178.         testMgr->TraceLogOff();
  179.     else
  180.         testMgr->TraceLogOn();
  181.         
  182.     testDynamicFunctions();
  183.     
  184.     // dump out TLibraryManager info
  185.     //testMgr->Dump();
  186.     
  187.     // Create our test object
  188.     if (verbose) cout << "***Creating object1" << endl;
  189.  
  190.     // Create object with classId = kTExampleClassID
  191.     TExampleClass* object1 = (TExampleClass*) testMgr->NewObject(ClassID(kTExampleClassID));
  192.     if (!object1)
  193.         cout << "Could not create new object!!!" << endl;
  194.  
  195.     //testMgr->Dump();
  196.     
  197.     if (verbose) cout << "***Creating object2" << endl;
  198.  
  199.     // Create object with classId = kExampleClass
  200.     // NOTE: We use the new (NULL) form of the new operator in case TExampleClass was compiled
  201.     // with Symantec C++ and we were compiled with MPW CFront.
  202.     //
  203.     TExampleClass* object2 = new (NULL) TExampleClass;
  204.     if (!object2)
  205.         cout << "Could not create new object!!!" << endl;
  206.  
  207.     /* Call the code that will actually do the tests. */
  208.  
  209.     cout << ends;
  210.     DoTests(object1, object2, theCount, trace, verbose, testClassInfoClass);
  211.     
  212.     if (verbose) cout << "***Disposing testMgr" << endl;
  213.     CleanupLibraryManager();    // delete the TLibraryManager and its pool
  214.  
  215.     return 0;
  216. };
  217.  
  218.  
  219. static int getIntArgument(char *arg)
  220. {
  221.     int theCount = -1;
  222.     
  223.     sscanf(arg, "%d", &theCount);
  224.         
  225.     return theCount;
  226. }
  227.  
  228.  
  229. void testDynamicFunctions()
  230. {
  231.     // Try out some dynamically linked functions
  232.     ulong startticks;
  233.     
  234.     cout << "Hello(ulong&):" << kHelloRef(startticks) << endl;
  235.     cout << "startticks = " << startticks << endl;
  236.  
  237.     cout << "Hello(ulong*):" << kHelloPtr(&startticks) << endl;
  238.     cout << "startticks = " << startticks << endl;
  239.  
  240.     cout << "HelloC(ulong*):" << HelloC(&startticks) << endl;
  241.     cout << "startticks = " << startticks << endl;
  242.  
  243. #ifdef THINK_CPLUS
  244.     cout << "HelloPascal(ulong&):" << HELLOPASCAL(startticks) << endl;
  245. #else
  246.     cout << "HelloPascal(ulong&):" << HelloPascal(startticks) << endl;
  247. #endif
  248.     cout << "startticks = " << startticks << endl;
  249.     
  250.     // Now lets call Hello a bunch of times
  251.     ulong startTestHelloCount;
  252.     ulong helloCount = kTestHelloCount;
  253.     kHelloRef(startTestHelloCount);
  254.     while (helloCount-- > 0)
  255.     {
  256.         ulong theTicks;
  257.         kHelloRef(theTicks);
  258.     }
  259.     ulong endTestHelloCount;
  260.     kHelloRef(endTestHelloCount);
  261.     cout << kTestHelloCount << " Iterations of Hello: " << endTestHelloCount - startTestHelloCount << endl;
  262.  
  263.     // Test the locally linked localHello() for a comparison
  264.     localHello(startTestHelloCount);
  265.     helloCount = kTestHelloCount;
  266.     while (helloCount-- > 0)
  267.     {
  268.         ulong theTicks;
  269.         localHello(theTicks);
  270.     }
  271.     localHello(endTestHelloCount);
  272.     cout << kTestHelloCount << " Iterations of local Hello: " << endTestHelloCount - startTestHelloCount << endl;
  273.  
  274.     cout << "Elapsed ticks (according to 'C' interface): " << Goodbye() << endl;
  275. #ifdef THINK_CPLUS
  276.     cout << "Elapsed ticks: (according to Pascal interface): " << GOODBYEPASCAL() << endl;
  277. #else
  278.     cout << "Elapsed ticks: (according to Pascal interface): " << GoodbyePascal() << endl;
  279. #endif
  280. }
  281.  
  282. char* localHello(ulong& theHelloTicks)
  283. {
  284.     helloticks = TickCount();
  285.     theHelloTicks = helloticks;
  286.     return "Hello";
  287. }
  288.  
  289.