home *** CD-ROM | disk | FTP | other *** search
/ RBBS in a Box Volume 1 #2 / RBBS_vol1_no2.iso / add2 / tavram.zip / TAVGRTST.PAS next >
Pascal/Delphi Source File  |  1989-05-30  |  8KB  |  331 lines

  1. Program VRamGraphTest;
  2. {$M 16384,50000,50000}
  3.  
  4. uses
  5.   Crt,
  6.   TaVram,
  7.   Graph;
  8. const
  9.   BGIspec = 'C:\LANG\TP5\BGI';
  10.   MaxUpdateSize = 200;
  11.   BarWidth = 70;
  12.   BarSpacing = 30;
  13.   BarDepth = 5;
  14.   BarXOffset = 20;
  15.   MemAvailBar        = 1;
  16.   MaxAvailBar        = 2;
  17.   BytesAllocatedBar  = 3;
  18.   MaxHeapToUseBar    = 4;
  19.   HeapUsedBar        = 5;
  20.   Labels : Array[MemAvailBar..HeapUsedBar] of String[20] = ( 'MemAvail',
  21.                                                              'MaxAvail',
  22.                                                              'BytesAlloc',
  23.                                                              'MaxHeap2Use',
  24.                                                              'HeapUsed');
  25. var
  26.   GraphDriver : Integer;
  27.   GraphMode : Integer;
  28.   ErrorCode : Integer;
  29.   MaxX,
  30.   MaxY,
  31.   GraphLeft,
  32.   GraphRight,
  33.   GraphTop,
  34.   GraphBot  : Integer;
  35.   VertFact : Real;
  36.   CurAlloc : LongInt;
  37.   LastAlloc,
  38.   LastMemAvail,
  39.   LastMaxAvail,
  40.   LastMaxHeapToUse,
  41.   LastHeapUsed : Array[0..1] of LongInt;
  42.   LargestBarSize : LongInt;
  43.   CurPage,
  44.   Pages : Word;
  45.  
  46. function LongStr(L : LongInt;P:Byte) : String;
  47. var
  48.   St : String[20];
  49. begin
  50.   Str(L:P,St);
  51.   LongStr:=St;
  52. end;
  53.  
  54. function RealStr(R : Real; P,D :Byte) : String;
  55. var
  56.   St : String[20];
  57. begin
  58.   Str(R:P:D,St);
  59.   RealStr:=St;
  60. end;
  61.  
  62. procedure SetFullViewPort;
  63. begin
  64.   SetViewPort(0,0,MaxX,MaxY,ClipOn);
  65. end;
  66.  
  67. procedure FlipPage;
  68. begin
  69.   SetActivePage(CurPage);
  70.   CurPage:=CurPage XOR $0001;
  71.   SetVisualPage(CurPage);
  72. end;
  73.  
  74. function DivideVert(X,Y1,Y2 : Integer; Lv,Hv : LongInt) : Real;
  75. var
  76.   R,
  77.   CR : Real;
  78.   CY : Integer;
  79. begin
  80.   R:=(Hv-Lv) / (Y2-Y1);
  81.   CY:=Y2;
  82.   CR:=0;
  83.   while CY>=Y1 do begin
  84.     SetColor(Cyan);
  85.     OutTextXY(X-(7*8),CY-4,RealStr(CR,6,0));
  86.     Line(X-2,CY,X+2,CY);
  87.     CR:=CR+(R*10);
  88.     Dec(CY,10);
  89.   end;
  90.   DivideVert:=R;
  91. end;
  92.  
  93. procedure ClearArea(X1,Y1,X2,Y2 : Word);
  94. begin
  95.   SetViewPort(X1,Y1,X2,Y2,True);
  96.   ClearViewPort;
  97.   SetFullViewPort;
  98. end;
  99.  
  100. procedure ClearStatusBar(N : Word);
  101. var
  102.   BarX : Integer;
  103. begin
  104.   BarX:=((N-1)*(BarWidth+BarSpacing))+(GraphLeft+BarXOffset);
  105.   ClearArea(BarX,0,BarX+BarWidth+BarDepth,GraphBot-1);
  106. end;
  107.  
  108. procedure DrawStatusBar(N : Word; Size : LongInt);
  109. var
  110.   BarX,
  111.   BarY : Integer;
  112.   UseSize : LongInt;
  113. begin
  114.   ClearStatusBar(N);
  115.   if Size<=0 then
  116.     Exit;
  117.   if Size>LargestBarSize then
  118.     UseSize:=LargestBarSize
  119.   else
  120.     UseSize:=Size;
  121.   BarY:=GraphBot-Round(UseSize/VertFact);
  122.   BarX:=((N-1)*(BarWidth+BarSpacing))+(GraphLeft+BarXOffset);
  123.   SetColor(LightGray);
  124.   SetFillStyle(LineFill+N,DarkGray+N);
  125.   Bar3D(BarX,BarY,BarX+BarWidth,GraphBot-1,BarDepth,True);
  126.   if UseSize<Size then begin
  127.     SetColor(DarkGray+N);
  128.     OutTextXY(BarX+8,0,#24+LongStr(Size,6)+#24);
  129.   end;
  130. end;
  131.  
  132. procedure DrawStatusBarLabel(N : Word; St : String);
  133. var
  134.   BarX : Integer;
  135. begin
  136.   BarX:=((N-1)*(BarWidth+BarSpacing))+(GraphLeft+BarXOffset);
  137.   SetColor(DarkGray+N);
  138.   OutTextXY(BarX,GraphBot+4,St);
  139. end;
  140.  
  141. procedure DrawLabels;
  142. var
  143.   W : Word;
  144. begin
  145.   For W:=MemAvailBar to HeapUsedBar do
  146.     DrawStatusBarLabel(W,Labels[W]);
  147. end;
  148.  
  149. procedure ClearStatusLine;
  150. begin
  151.   ClearArea(0,MaxY-10,MaxX,MaxY);
  152. end;
  153.  
  154. procedure DisplayStatusLine(St : String);
  155. begin
  156.   ClearStatusLine;
  157.   SetColor(LightGreen);
  158.   OutTextXY(0,MaxY-10,St);
  159. end;
  160.  
  161. procedure UpdateGraphs;
  162. var
  163.   MaxAv,
  164.   MemAv : LongInt;
  165. begin
  166.   MemAv:=MemAvail;
  167.   MaxAv:=MaxAvail;
  168.   if Abs(LastMemAvail[CurPage]-MemAv)>MaxUpdateSize then begin
  169.     DrawStatusBar(MemAvailBar,MemAv);
  170.     LastMemAvail[CurPage]:=MemAv;
  171.   end;
  172.   if Abs(LastMaxAvail[CurPage]-MaxAv)>MaxUpdateSize then begin
  173.     DrawStatusBar(MaxAvailBar,MaxAv);
  174.     LastMaxAvail[CurPage]:=MaxAv;
  175.   end;
  176.   if Abs(LastAlloc[CurPage]-CurAlloc)>=MaxUpdateSize then begin
  177.     DrawStatusBar(BytesAllocatedBar,CurAlloc);
  178.     LastAlloc[CurPage]:=CurAlloc;
  179.   end;
  180.   if Abs(LastMaxHeapToUse[CurPage]-VRamMaxHeapToUse)>MaxUpdateSize then begin
  181.     DrawStatusBar(MaxHeapToUseBar,VRamMaxHeapToUse);
  182.     LastMaxHeapToUse[CurPage]:=VRamMaxHeapToUse;
  183.   end;
  184.   if Abs(LastHeapUsed[CurPage]-VRamHeapUsed)>MaxUpdateSize then begin
  185.     DrawStatusBar(HeapUsedBar,VRamHeapUsed);
  186.     LastHeapUsed[CurPage]:=VRamHeapUsed;
  187.   end;
  188. end;
  189.  
  190. procedure DisplayStatusUpdate(St : String);
  191. begin
  192.   DisplayStatusLine(St);
  193.   UpdateGraphs;
  194.   if Pages>0 then
  195.     FlipPage;
  196. end;
  197.  
  198. procedure SetupLastSizes;
  199. var
  200.   i : Integer;
  201. begin
  202.   for i:=0 to 1 do begin
  203.     LastAlloc[i]:=-MaxUpdateSize;
  204.     LastMemAvail[i]:=-MaxUpdateSize;
  205.     LastMaxAvail[i]:=-MaxUpdateSize;
  206.     LastMaxHeapToUse[i]:=-MaxUpdateSize;
  207.     LastHeapUsed[i]:=-MaxUpdateSize;
  208.   end;
  209. end;
  210.  
  211. procedure Init;
  212. var
  213.   W : Word;
  214. begin
  215.   DetectGraph(GraphDriver,GraphMode);
  216.   if GraphDriver in[EGA,HercMono,VGA] then begin
  217.     Pages:=1;
  218.     Case GraphDriver of
  219.       EGA : GraphMode:=EGAHi;
  220.       HercMono : GraphMode:=HercMonoHi;
  221.       VGA : GraphMode:=VGAMed;
  222.       else Pages:=0;
  223.     end;
  224.   end;
  225.   InitGraph(GraphDriver,GraphMode,BGIspec);
  226.   if GraphResult<>grOk then begin
  227.     writeln;
  228.     writeln('Turbo Pascal Graph error #',GraphResult);
  229.     writeln('Program aborted.');
  230.     Halt(1);
  231.   end;
  232.   MaxX:=GetMaxX;
  233.   MaxY:=GetMaxY;
  234.   GraphLeft:=60;
  235.   GraphRight:=MaxX-10;
  236.   GraphTop:=12;
  237.   GraphBot:=MaxY-30;
  238.   CurAlloc:=0;
  239.   CurPage:=0;
  240.  
  241.   W:=0;
  242.   While W<=Pages do begin
  243.     SetActivePage(W);
  244.     SetBkColor(Black);
  245.     ClearViewPort;
  246.     SetColor(Yellow);
  247.     SetLineStyle(SolidLn,0,NormWidth);
  248.     Line(GraphLeft,GraphTop,GraphLeft,GraphBot);
  249.     Line(GraphLeft,GraphBot,GraphRight,GraphBot);
  250.     LargestBarSize:=MemAvail;
  251.     VertFact:=DivideVert(GraphLeft,GraphTop,GraphBot,0,LargestBarSize);
  252.     DrawLabels;
  253.     Inc(W);
  254.   end;
  255.   SetupLastSizes;
  256. end;
  257.  
  258. procedure Test;
  259. const
  260.   MaxBigArray = 255;
  261.   MaxArraySize = 400;
  262. type
  263.   BigRecPtr = ^BigRec;
  264.   BigRec = Record
  265.     A: Array[1..MaxArraySize] of byte;
  266.   end;
  267. var
  268.   BRArray : Array[1..MaxBigArray] of BigRecPtr;
  269.   I,J : integer;
  270.   Temp : LongInt;
  271.   ch : Char;
  272.  
  273. procedure AbortTest;
  274. begin
  275.   CloseGraph;
  276.   writeln('Test failed.');
  277.   Halt(1);
  278. end;
  279.  
  280. begin
  281.   DisplayStatusUpdate('Press a key to start.');
  282.   ch:=ReadKey;
  283. {$P+}
  284.   VRamOn;
  285.   VRamMaxHeapToUse:=20000;
  286.   { FreeMin:=(MaxBigArray * 4) * 2;   absolute safest if not using
  287.                                       VRamMaxHeapToUse.            }
  288.   VRamPageOff;
  289.   for i:=1 to MaxBigArray do begin
  290.     New(BRArray[i]);
  291.     Inc(CurAlloc,SizeOf(BigRec));
  292.     FillChar(BRArray[i]^,MaxArraySize,i);
  293.     if i>60 then  {Watch VRamHeapUsed go way above VRamMaxHeapToUse.}
  294.       VRamPageOn;  {then watch it pop back down.}
  295.     DisplayStatusUpdate('Allocating #'+LongStr(i,3));
  296.   end;
  297.   while not KeyPressed do begin
  298.     i:=Random(MaxBigArray)+1;
  299.     for j:=1 to MaxArraySize do
  300.       if BRArray[i]^.A[j]<>i then
  301.         AbortTest;
  302.     DisplayStatusUpdate('Testing #'+LongStr(i,3)+'... OK!');
  303.   end;
  304.   while KeyPressed do ch:=ReadKey;
  305.   for i:=50 to 100 do begin
  306.     Dispose(BRArray[i]);
  307.     Dec(CurAlloc,SizeOf(BigRec));
  308.     DisplayStatusUpdate('Deallocated # '+LongStr(i,3));
  309.   end;
  310.   for i:=1 to 49 do begin
  311.     Dispose(BRArray[i]);
  312.     Dec(CurAlloc,SizeOf(BigRec));
  313.     DisplayStatusUpdate('Deallocated #'+LongStr(i,3));
  314.   end;
  315.   for i:=101 to MaxBigArray do begin
  316.     Dispose(BRArray[i]);
  317.     Dec(CurAlloc,SizeOf(BigRec));
  318.     DisplayStatusUpdate('Deallocated #'+LongStr(i,3));
  319.   end;
  320. {$P-}
  321.   DisplayStatusUpdate('FINISHED!  Press a key to continue.');
  322.   ch:=ReadKey;
  323. end;
  324.  
  325. begin
  326.   Init;
  327.   Test;
  328.   CloseGraph;
  329. end.
  330.  
  331.