home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.pdx.edu / 2014.02.ftp.ee.pdx.edu.tar / ftp.ee.pdx.edu / pub / users / Harry / Blitz / OSProject / p8 / TestProgram4.c < prev    next >
Text File  |  2007-09-19  |  45KB  |  1,449 lines

  1. code TestProgram4
  2.  
  3.  
  4.            ------------------------------------------
  5.            ------                              ------
  6.            ------  Harry's Test Program #4     ------
  7.            ------                              ------
  8.            ------------------------------------------
  9.  
  10.  
  11. -----------------------------  main  ---------------------------------
  12.  
  13.   function main ()
  14.     --
  15.     -- For each of the following tests, you should uncomment the call,
  16.     -- compile and run this program, and hand in the output it produces.
  17.     --
  18.     -- This program should be run with the following constants in the kernel:
  19.     --     NUMBER_OF_PHYSICAL_PAGE_FRAMES = 512
  20.     --     MAX_NUMBER_OF_PROCESSES = 10
  21.     --     MAX_PAGES_PER_VIRT_SPACE = 20
  22.     --     MAX_FILES_PER_PROCESS = 10
  23.     --     USER_STACK_SIZE_IN_PAGES = 1
  24.     --     NUMBER_OF_ENVIRONMENT_PAGES = 0
  25.  
  26.       -- OpenTest1 ()
  27.       -- OpenTest2 ()
  28.       -- OpenCloseTest ()
  29.       -- CloseTest ()
  30.       -- ReadTest1 ()
  31.       -- ReadTest2 ()
  32.       -- SeekTest ()
  33.       -- WriteTest1 ()
  34.       -- WriteTest2 ()
  35.       -- WriteTest3 ()
  36.       -- WriteTest4 ()
  37.       -- WriteTest5 ()
  38.       -- ReadTest3 ()
  39.       -- ReadTest4 ()
  40.       -- WriteTest6 ()
  41.       -- OpenTest3 ()
  42.       -- ExecTest1 ()
  43.       -- ExecTest2 ()
  44.       -- ExecTest3 ()
  45.  
  46.       Sys_Exit (0)
  47.     endFunction
  48.  
  49.  
  50. -----------------------------  OpenTest1  ---------------------------------
  51.  
  52.   function OpenTest1 ()
  53.     --
  54.     -- Basic tests of Sys_Open.
  55.     --
  56.       var fd: int
  57.       print ("\n**********  OpenTest1 running  **********\n\n")
  58.  
  59.       print ("Attempting to open a nonexistent file, which should return -1.\n")
  60.       fd = Sys_Open ("NotThere")
  61.       if fd == -1
  62.         print ("Okay.\n")
  63.       elseIf fd < 0
  64.         print ("*****  ERROR: Open returned a negative number ( ")
  65.         printInt (fd)
  66.         print (") other than -1  *****\n")
  67.       else
  68.         print ("*****  ERROR: Open returned a non-negative number ( ")
  69.         printInt (fd)
  70.         print (") indicating the open succeeded  *****\n")
  71.       endIf
  72.  
  73.       print ("Attempting to open a file with an overly large name, which should return -1.\n")
  74.       fd = Sys_Open ("NameOfNonexistingFileeeeeeeeeeeeeeeeeeeeeeee")
  75.       if fd == -1
  76.         print ("Okay.\n")
  77.       elseIf fd < 0
  78.         print ("*****  ERROR: Open returned a negative number ( ")
  79.         printInt (fd)
  80.         print (") other than -1  *****\n")
  81.       else
  82.         print ("*****  ERROR: Open returned a non-negative number ( ")
  83.         printInt (fd)
  84.         print (") indicating the open succeeded  *****\n")
  85.       endIf
  86.  
  87.       print ("Attempting to open an existing file, which should succeed.\n")
  88.       fd = Sys_Open ("file1")
  89.       if fd < 0
  90.         print ("*****  ERROR: Open returned a negative number ( ")
  91.         printInt (fd)
  92.         print (") indicating the open failed  *****\n")
  93.       elseIf fd == 0
  94.         print ("Okay.\n")
  95.       else
  96.         print ("*** WARNING: The syscall returns ")
  97.         printInt (fd)
  98.         print (", which is perhaps correct but I was expecting 0 for the first open fd ***\n")
  99.       endIf
  100.  
  101.       print ("\n**********  Test Complete  **********\n\n")
  102.  
  103.     endFunction
  104.  
  105. -----------------------------  OpenTest2  ---------------------------------
  106.  
  107.   function OpenTest2 ()
  108.     --
  109.     -- Attempt to open too many files.
  110.     --
  111.       var i, fd: int
  112.       print ("\n**********  OpenTest2 running  **********\n\n")
  113.  
  114.  
  115.       print ("Attempting to open the same file 10 times.\n")
  116.       for i = 0 to 9
  117.         fd = Sys_Open ("file1")
  118.         if fd < 0
  119.           print ("*****  ERROR: Open returned a negative number ( ")
  120.           printInt (fd)
  121.           print (") indicating the open failed  *****\n")
  122.         elseIf fd == i  -- Should get a new fd each time!
  123.           print ("Open ")
  124.           printInt (fd)
  125.           print (" succeeded\n")
  126.         else
  127.           print ("*** WARNING: The syscall returns ")
  128.           printInt (fd)
  129.           print (", which is an unexpected (but possibly correct) fd ***\n")
  130.         endIf
  131.       endFor
  132.  
  133.       print ("Attempting to open the file one more time, which should return -1.\n")
  134.       fd = Sys_Open ("file1")
  135.       if fd == -1
  136.         print ("Okay.\n")
  137.       elseIf fd < 0
  138.         print ("*****  ERROR: Open returned a negative number ( ")
  139.         printInt (fd)
  140.         print (") other than -1  *****\n")
  141.       else
  142.         print ("*****  ERROR: Open returned a non-negative number ( ")
  143.         printInt (fd)
  144.         print (") indicating the open succeeded  *****\n")
  145.       endIf
  146.  
  147.       print ("\n**********  Test Complete  **********\n\n")
  148.  
  149.     endFunction
  150.  
  151. -----------------------------  OpenCloseTest  ---------------------------------
  152.  
  153.   function OpenCloseTest ()
  154.     --
  155.     -- Attempt to open and close a file many times.
  156.     --
  157.       var i, j, fd: int
  158.       print ("\n**********  OpenCloseTest running  **********\n\n")
  159.  
  160.       for j = 1 to 10
  161.         print ("Opening 10 files...\n")
  162.         for i = 0 to 9
  163.           fd = Sys_Open ("file1")
  164.           if fd < 0
  165.             print ("*****  ERROR: Open returned a negative number ( ")
  166.             printInt (fd)
  167.             print (") indicating the open failed  *****\n")
  168.           elseIf fd == i  -- Should get a new fd each time!
  169.             -- print ("Open ")
  170.             -- printInt (fd)
  171.             -- print (" succeeded\n")
  172.           else
  173.             print ("*** WARNING: The syscall returns ")
  174.             printInt (fd)
  175.             print (", which is an unexpected (but possibly correct) fd ***\n")
  176.           endIf
  177.         endFor
  178.  
  179.         -- Close all 10 fileDesctiptors.
  180.         print ("Closing all 10 files...\n")
  181.         for i = 0 to 9
  182.           Sys_Close (i)
  183.         endFor
  184.       endFor
  185.  
  186.       print ("\n**********  Test Complete  **********\n\n")
  187.  
  188.     endFunction
  189.  
  190. -----------------------------  CloseTest  ---------------------------------
  191.  
  192.   function CloseTest ()
  193.     --
  194.     -- Attempt close some files which are not open.
  195.     --
  196.       print ("\n**********  CloseTest running  **********\n\n")
  197.       print ("Closing files which are not open and invalid file descriptors; should be nops...\n")
  198.  
  199.  
  200.       Sys_Close (0)
  201.       Sys_Close (1)
  202.       Sys_Close (9)
  203.       Sys_Close (-1)
  204.       Sys_Close (-12345)
  205.       Sys_Close (12345)
  206.  
  207.       print ("\n**********  Test Complete  **********\n\n")
  208.  
  209.     endFunction
  210.  
  211. -----------------------------  ReadTest1  ---------------------------------
  212.  
  213.   var buffer: array [30] of char = new array of char { 30 of '?' }
  214.  
  215.   function ReadTest1 ()
  216.     --
  217.     -- This is a basic test of the Read syscall.
  218.     --
  219.       var fd, i: int
  220.           charPtr: ptr to char = &buffer[0]
  221.  
  222.       print ("\n**********  ReadTest1 running  **********\n\n")
  223.  
  224.       fd = Sys_Open ("file1")
  225.       i = Sys_Read (fd, charPtr, 30)
  226.       print ("\nShould print '>>>Now is the time for all good m<<<'\n")
  227.       print ("              >>>")
  228.       print (& buffer)
  229.       print ("<<<\n")
  230.       if i != 30
  231.         print ("*****  ERROR: return value from Sys_Read (")
  232.         printInt (i)
  233.         print (") is incorrect  *****\n")
  234.       endIf
  235.  
  236.       i = Sys_Read (fd, charPtr, 30)
  237.       print ("\nShould print '>>>en to come to the aid of their<<<'\n")
  238.       print ("              >>>")
  239.       print (& buffer)
  240.       print ("<<<\n")
  241.       if i != 30
  242.         print ("*****  ERROR: return value from Sys_Read (")
  243.         printInt (i)
  244.         print (") is incorrect  *****\n")
  245.       endIf
  246.  
  247.       i = Sys_Read (fd, charPtr, 30)
  248.       print ("\nShould print '>>> parties!!!\no the aid of their<<<'\n")
  249.       print ("              >>>")
  250.       print (& buffer)
  251.       print ("<<<\n")
  252.       if i != 12
  253.         print ("*****  ERROR: return value from Sys_Read (")
  254.         printInt (i)
  255.         print (") is incorrect  *****\n")
  256.       endIf
  257.  
  258.       -- Now that we are at the end of the file, try another read.
  259.       -- It should not update the buffer.
  260.       i = Sys_Read (fd, charPtr, 30)
  261.       print ("\nShould print '>>> parties!!!\no the aid of their<<<'\n")
  262.       print ("              >>>")
  263.       print (& buffer)
  264.       print ("<<<\n")
  265.       if i != 0
  266.         print ("*****  ERROR: return value from Sys_Read (")
  267.         printInt (i)
  268.         print (") is incorrect  *****\n")
  269.       endIf
  270.  
  271.       -- Don't bother closing the file...
  272.  
  273.       print ("\n**********  Test Complete  **********\n\n")
  274.  
  275.     endFunction
  276.  
  277. -----------------------------  ReadTest2  ---------------------------------
  278.  
  279.   function ReadTest2 ()
  280.     --
  281.     -- This is tests error conditions on the Read syscall.
  282.     --
  283.       var fd, i: int
  284.           charPtr: ptr to char = &buffer[0]
  285.  
  286.       print ("\n**********  ReadTest2 running  **********\n\n")
  287.  
  288.       print ("Reading files which are not open and invalid file descriptors...\n")
  289.  
  290.       i = Sys_Read (0, charPtr, 30)
  291.       if i == -1
  292.         print ("Okay\n")
  293.       else
  294.         print ("*****  ERROR: return value from Sys_Read (")
  295.         printInt (i)
  296.         print (") is incorrect  *****\n")
  297.       endIf
  298.  
  299.       i = Sys_Read (1, charPtr, 30)
  300.       if i == -1
  301.         print ("Okay\n")
  302.       else
  303.         print ("*****  ERROR: return value from Sys_Read (")
  304.         printInt (i)
  305.         print (") is incorrect  *****\n")
  306.       endIf
  307.  
  308.       i = Sys_Read (9, charPtr, 30)
  309.       if i == -1
  310.         print ("Okay\n")
  311.       else
  312.         print ("*****  ERROR: return value from Sys_Read (")
  313.         printInt (i)
  314.         print (") is incorrect  *****\n")
  315.       endIf
  316.  
  317.       i = Sys_Read (-1, charPtr, 30)
  318.       if i == -1
  319.         print ("Okay\n")
  320.       else
  321.         print ("*****  ERROR: return value from Sys_Read (")
  322.         printInt (i)
  323.         print (") is incorrect  *****\n")
  324.       endIf
  325.  
  326.       i = Sys_Read (-12345, charPtr, 30)
  327.       if i == -1
  328.         print ("Okay\n")
  329.       else
  330.         print ("*****  ERROR: return value from Sys_Read (")
  331.         printInt (i)
  332.         print (") is incorrect  *****\n")
  333.       endIf
  334.  
  335.       i = Sys_Read (12345, charPtr, 30)
  336.       if i == -1
  337.         print ("Okay\n")
  338.       else
  339.         print ("*****  ERROR: return value from Sys_Read (")
  340.         printInt (i)
  341.         print (") is incorrect  *****\n")
  342.       endIf
  343.  
  344.       fd = Sys_Open ("file1")
  345.  
  346.       print ("Reading with negative sizeInBytes...\n")
  347.  
  348.       i = Sys_Read (0, charPtr, -1)
  349.       if i == -1
  350.         print ("Okay\n")
  351.       else
  352.         print ("*****  ERROR: return value from Sys_Read (")
  353.         printInt (i)
  354.         print (") is incorrect  *****\n")
  355.       endIf
  356.  
  357.       i = Sys_Read (0, charPtr, -123123123)
  358.       if i == -1
  359.         print ("Okay\n")
  360.       else
  361.         print ("*****  ERROR: return value from Sys_Read (")
  362.         printInt (i)
  363.         print (") is incorrect  *****\n")
  364.       endIf
  365.  
  366.       print ("\n**********  Test Complete  **********\n\n")
  367.  
  368.     endFunction
  369.  
  370. -----------------------------  ReadTest3  ---------------------------------
  371.  
  372.   var buffer2: array [10] of char = new array of char { 10 of '-' }
  373.  
  374.   function ReadTest3 ()
  375.     --
  376.     -- This is tests that a file open in the parent will be readable in child.
  377.     --
  378.       var fd, i, j, pid: int
  379.           charPtr: ptr to char
  380.  
  381.       print ("\n**********  ReadTest3 running  **********\n\n")
  382.  
  383.       fd = Sys_Open ("file1")
  384.       buffer2 [9] = '\n'
  385.  
  386.       print ("Should print 9 lines with the characters 'Now is th'.\n")
  387.       print ("Each line should contain only one character.\n")
  388.  
  389.       -- Fork off 9 children...
  390.       for i = 1 to 9
  391.         pid = Sys_Fork ()
  392.         if pid == 0
  393.           -- Child: get next character and close the file...
  394.           charPtr = &buffer2[i-1]
  395.           j = Sys_Read (fd, charPtr, 1)
  396.           Sys_Close (fd)
  397.           if j != 1
  398.             print ("*****  ERROR: Return value from Sys_Read (")
  399.             printInt (j)
  400.             print (") is incorrect  *****\n")
  401.           endIf
  402.           print (&buffer2)
  403.           Sys_Exit (0)
  404.         endIf
  405.         -- After forking off child, wait for it to complete, before stating next...
  406.         j = Sys_Join (pid)
  407.       endFor
  408.  
  409.       buffer2 = new array of char { 10 of '-' }
  410.       buffer2 [9] = '\n'
  411.  
  412.       print ("Should print 9 lines with the characters 'e time fo'.\n")
  413.       print ("Each line should contain only one character.\n")
  414.  
  415.       -- Fork off 9 more children (to make sure all resources are freed)...
  416.       for i = 1 to 9
  417.         pid = Sys_Fork ()
  418.         if pid == 0
  419.           -- Child: get next character and close the file...
  420.           charPtr = &buffer2[i-1]
  421.           j = Sys_Read (fd, charPtr, 1)
  422.           Sys_Close (fd)
  423.           if j != 1
  424.             print ("*****  ERROR: Return value from Sys_Read (")
  425.             printInt (j)
  426.             print (") is incorrect  *****\n")
  427.           endIf
  428.           print (&buffer2)
  429.           Sys_Exit (0)
  430.         endIf
  431.         -- After forking off child, wait for it to complete, before stating next...
  432.         j = Sys_Join (pid)
  433.       endFor
  434.  
  435.       buffer2 = new array of char { 10 of '-' }
  436.       buffer2 [9] = '\n'
  437.  
  438.       print ("Should print 9 lines with the characters 'r all goo'.\n")
  439.       print ("Each line should contain only one character.\n")
  440.  
  441.       -- Fork off 9 more children (to make sure all resources are freed)...
  442.       for i = 1 to 9
  443.         pid = Sys_Fork ()
  444.         if pid == 0
  445.           -- Child: get next character and close the file...
  446.           charPtr = &buffer2[i-1]
  447.           j = Sys_Read (fd, charPtr, 1)
  448.           Sys_Close (fd)
  449.           if j != 1
  450.             print ("*****  ERROR: Return value from Sys_Read (")
  451.             printInt (j)
  452.             print (") is incorrect  *****\n")
  453.           endIf
  454.           print (&buffer2)
  455.           Sys_Exit (0)
  456.         endIf
  457.         -- After forking off child, wait for it to complete, before stating next...
  458.         j = Sys_Join (pid)
  459.       endFor
  460.  
  461.       print ("\n**********  Test Complete  **********\n\n")
  462.  
  463.       Sys_Exit (0)
  464.  
  465.     endFunction
  466.  
  467. -----------------------------  ReadTest4  ---------------------------------
  468.  
  469.   function ReadTest4 ()
  470.     --
  471.     -- This tests a number of strange conditions on the Read syscall.
  472.     --
  473.       var fd, i, p, save: int
  474.  
  475.       print ("\n**********  ReadTest4 running  **********\n\n")
  476.  
  477.       fd = Sys_Open ("file1")
  478.  
  479.       -- Seek to end of file
  480.       i = Sys_Seek (fd, -1)
  481.  
  482.       -- Try to read a byte, which should read 0 bytes.
  483.       i = Sys_Read (fd, &buffer2[0], 1)
  484.       Check (i, 0)
  485.  
  486.       -- Try to read several bytes, which should read 0 bytes.
  487.       i = Sys_Read (fd, &buffer2[0], 5)
  488.       Check (i, 0)
  489.  
  490.       -- Try to read zero bytes, which should be okay.
  491.       i = Sys_Read (fd, &buffer2[0], 0)
  492.       Check (i, 0)
  493.  
  494.       -- Now seek to beginning of file
  495.       i = Sys_Seek (fd, 0)
  496.  
  497.       -- Try to read -1 bytes, which should be an error.
  498.       i = Sys_Read (fd, &buffer2[0], -1)
  499.       Check (i, -1)
  500.  
  501.       -- Try to read -5 bytes, which should be an error.
  502.       i = Sys_Read (fd, &buffer2[0], -5)
  503.       Check (i, -1)
  504.  
  505.       -- Use a pointer which isn't in our address space, which should be an error.
  506.       i = Sys_Read (fd, (-1) asPtrTo char, 1)
  507.       Check (i, -1)
  508.  
  509.       -- Use a pointer to a page which is read-only, which should be an error.
  510.       i = Sys_Read (fd, main asPtrTo char, 1)
  511.       Check (i, -1)
  512.  
  513.       -- Use a pointer which isn't in our address space, which should be an error.
  514.       i = Sys_Read (fd, 0x0fffffff asPtrTo char, 1)
  515.       Check (i, -1)
  516.  
  517.       -- Next get a pointer to the last word in our address space.
  518.       -- (This is done by first finding the page containing the stack.
  519.       p = ((&fd) asInteger & 0xffffe000) | 0x00001ffc
  520.       -- printHexVar ("Virtual addr of last word in address space", p)
  521.  
  522.       -- Try a read into the last 4 bytes of the address space.  Should be okay.
  523.       -- Also, save and restore the word that was updated.
  524.       save = *(p asPtrTo int)
  525.       i = Sys_Read (fd, p asPtrTo char, 4)
  526.       if save == *(p asPtrTo int)
  527.         print ("*****  ERROR: Did not change last 4 bytes  *****\n")
  528.       endIf
  529.       *(p asPtrTo int) = save
  530.       Check (i, 4)
  531.  
  532.       -- Now check that we are positioned correctly in the file.  Only
  533.       -- the last read should have changed it and it should be positioned
  534.       --       "Now <HERE>is the time for all..."
  535.       i = Sys_Read (fd, &buffer2[0], 10)
  536.       Check (i, 10)
  537.       if MemoryEqual (&buffer2[0], &("is the tim" [0]), 10)
  538.         print ("Okay.\n")
  539.       else
  540.         print ("*****  ERROR: Current position is not what is expected!  *****\n")
  541.         print (&buffer2)
  542.         nl ()
  543.       endIf
  544.  
  545.       -- Next, try a read of 5 bytes to the last word of our address space.
  546.       -- The first 4 bytes should be okay but then there should be a problem.
  547.       -- The OS should either
  548.       --        Read 4 bytes and return 4
  549.       --        Read 0 bytes and return -1
  550.       -- (The spec is not clear about which should happen.)
  551.       save = *(p asPtrTo int)
  552.       i = Sys_Read (fd, p asPtrTo char, 5)
  553.       if i == 4
  554.         if save == *(p asPtrTo int)
  555.           print ("*****  ERROR: Return code is 4, but did not change last 4 bytes  *****\n")
  556.         else
  557.           print ("Okay.\n")
  558.         endIf
  559.       elseIf i == -1
  560.         if save != *(p asPtrTo int)
  561.           print ("*****  ERROR: Return code is -1 but did change the last 4 bytes  *****\n")
  562.         else
  563.           print ("Okay.\n")
  564.         endIf
  565.       else
  566.         Check (i, -1)
  567.       endIf
  568.       *(p asPtrTo int) = save
  569.        
  570.       print ("\n**********  Test Complete  **********\n\n")
  571.  
  572.     endFunction
  573.  
  574. -----------------------------  SeekTest  ---------------------------------
  575.  
  576.   function SeekTest ()
  577.     --
  578.     -- This is a basic test of the Seek syscall.
  579.     --
  580.       var fd, i, retval: int
  581.  
  582.       print ("\n**********  SeekTest running  **********\n\n")
  583.  
  584.       fd = Sys_Open ("file1")
  585.  
  586.       print ("Should print 4...\n")
  587.       retval = Sys_Seek (fd, 4)
  588.       if retval != 4
  589.         print ("\n\n*****  ERROR  *****\n\n")
  590.       endIf
  591.       printIntVar ("retval", retval)
  592.  
  593.       print ("Should print 0...\n")
  594.       retval = Sys_Seek (fd, 0)
  595.       if retval != 0
  596.         print ("\n\n*****  ERROR  *****\n\n")
  597.       endIf
  598.       printIntVar ("retval", retval)
  599.  
  600.       print ("Should print 72...\n")
  601.       retval = Sys_Seek (fd, -1)
  602.       if retval != 72
  603.         print ("\n\n*****  ERROR  *****\n\n")
  604.       endIf
  605.       printIntVar ("retval", retval)
  606.  
  607.       print ("Should print -1 (newCurrentPos is > file size)...\n")
  608.       retval = Sys_Seek (fd, 73)
  609.       if retval != -1
  610.         print ("\n\n*****  ERROR  *****\n\n")
  611.       endIf
  612.       printIntVar ("retval", retval)
  613.  
  614.       print ("Should print -1 (newCurrentPos is < -1)...\n")
  615.       retval = Sys_Seek (fd, -2)
  616.       if retval != -1
  617.         print ("\n\n*****  ERROR  *****\n\n")
  618.       endIf
  619.       printIntVar ("retval", retval)
  620.  
  621.       print ("Should print -1 (file not open)...\n")
  622.       retval = Sys_Seek (3, 4)
  623.       if retval != -1
  624.         print ("\n\n*****  ERROR  *****\n\n")
  625.       endIf
  626.       printIntVar ("retval", retval)
  627.  
  628.       print ("Should print -1 (bad file descriptor)...\n")
  629.       retval = Sys_Seek (-1, 4)
  630.       if retval != -1
  631.         print ("\n\n*****  ERROR  *****\n\n")
  632.       endIf
  633.       printIntVar ("retval", retval)
  634.  
  635.       print ("Should print 35...\n")
  636.       retval = Sys_Seek (fd, 35)
  637.       if retval != 35
  638.         print ("\n\n*****  ERROR  *****\n\n")
  639.       endIf
  640.       printIntVar ("retval", retval)
  641.  
  642.       i = Sys_Read (fd, &buffer[0], 30)
  643.       print ("\nShould print '>>> come to the aid of their part<<<'\n")
  644.       print ("              >>>")
  645.       print (& buffer)
  646.       print ("<<<\n")
  647.       if i != 30
  648.         print ("*****  ERROR: return value from Sys_Read (")
  649.         printInt (i)
  650.         print (") is incorrect  *****\n")
  651.       endIf
  652.  
  653.       print ("\nShould print 8...\n")
  654.       retval = Sys_Seek (fd, 8)
  655.       if retval != 8
  656.         print ("\n\n*****  ERROR  *****\n\n")
  657.       endIf
  658.       printIntVar ("retval", retval)
  659.  
  660.       i = Sys_Read (fd, &buffer[0], 30)
  661.       print ("\nShould print '>>>he time for all good men to co<<<'\n")
  662.       print ("              >>>")
  663.       print (& buffer)
  664.       print ("<<<\n")
  665.       if i != 30
  666.         print ("*****  ERROR: return value from Sys_Read (")
  667.         printInt (i)
  668.         print (") is incorrect  *****\n")
  669.       endIf
  670.  
  671.       print ("\n**********  Test Complete  **********\n\n")
  672.  
  673.     endFunction
  674.  
  675. -----------------------------  WriteTest1  ---------------------------------
  676.  
  677.   var buffer3: array [20] of char = new array of char { 20 of '?' }
  678.       readBuff: array [20] of char = new array of char { 20 of '?' }
  679.       buffer4: array [20] of char = new array of char { 20 of '?' }
  680.  
  681.   function WriteTest1 ()
  682.     --
  683.     -- This is a basic test of the write operation.
  684.     --
  685.       var fd, i: int
  686.  
  687.       print ("\n**********  WriteTest1 running  **********\n\n")
  688.       buffer3 = * "abcdefghijklmnopqrst"
  689.  
  690.       fd = Sys_Open ("file2")
  691.       i = Sys_Write (fd, &buffer3[0], 20)
  692.       if i != 20
  693.         print ("*****  ERROR: return value from Sys_Write (")
  694.         printInt (i)
  695.         print (") is incorrect  *****\n")
  696.       endIf
  697.  
  698.       -- Read entire file and print it...
  699.       i = Sys_Seek (fd, 0)
  700.       if i != 0
  701.         print ("*****  ERROR: return value from Sys_Seek (")
  702.         printInt (i)
  703.         print (") is incorrect  *****\n")
  704.       endIf
  705.       i = Sys_Read (fd, &readBuff[0], 20)
  706.       if i != 20
  707.         print ("*****  ERROR: return value from Sys_Read (")
  708.         printInt (i)
  709.         print (") is incorrect  *****\n")
  710.       endIf
  711.       print ("\nShould print '>>>abcdefghijklmnopqrst<<<'\n")
  712.       print ("              >>>")
  713.       print (& readBuff)
  714.       print ("<<<\n")
  715.  
  716.       -- Print an error message if problems...
  717.       CheckFile (fd, true, 0, "abcdefghijklmnopqrst")
  718.  
  719.       buffer4 = * "ZYXWVUTSRQPONMLKJIHG"
  720.  
  721.       -- Write "ZYXWV" at location 13...
  722.       i = Sys_Seek (fd, 13)
  723.       if i != 13
  724.         print ("*****  ERROR: return value from Sys_Seek (")
  725.         printInt (i)
  726.         print (") is incorrect  *****\n")
  727.       endIf
  728.       i = Sys_Write (fd, &buffer4[0], 5)
  729.       if i != 5
  730.         print ("*****  ERROR: return value from Sys_Write (")
  731.         printInt (i)
  732.         print (") is incorrect  *****\n")
  733.       endIf
  734.  
  735.       -- Write "PONML" at location 5...
  736.       i = Sys_Seek (fd, 5)
  737.       if i != 5
  738.         print ("*****  ERROR: return value from Sys_Seek (")
  739.         printInt (i)
  740.         print (") is incorrect  *****\n")
  741.       endIf
  742.       i = Sys_Write (fd, &buffer4[10], 5)
  743.       if i != 5
  744.         print ("*****  ERROR: return value from Sys_Write (")
  745.         printInt (i)
  746.         print (") is incorrect  *****\n")
  747.       endIf
  748.  
  749.       -- Read entire file and print it...
  750.       i = Sys_Seek (fd, 0)
  751.       if i != 0
  752.         print ("*****  ERROR: return value from Sys_Seek (")
  753.         printInt (i)
  754.         print (") is incorrect  *****\n")
  755.       endIf
  756.       i = Sys_Read (fd, &readBuff[0], 20)
  757.       if i != 20
  758.         print ("*****  ERROR: return value from Sys_Read (")
  759.         printInt (i)
  760.         print (") is incorrect  *****\n")
  761.       endIf
  762.       print ("\nShould print '>>>abcdePONMLklmZYXWVst<<<'\n")
  763.       print ("              >>>")
  764.       print (& readBuff)
  765.       print ("<<<\n")
  766.  
  767.       -- Print an error message if problems...
  768.       CheckFile (fd, true, 0, "abcdePONMLklmZYXWVst")
  769.  
  770.       print ("\n**********  Test Complete  **********\n\n")
  771.  
  772.     endFunction
  773.  
  774. -----------------------------  WriteTest2  ---------------------------------
  775.  
  776.   function WriteTest2 ()
  777.     --
  778.     -- This is tests error conditions on the write syscall.
  779.     --
  780.       var fd, i: int
  781.  
  782.       print ("\n**********  WriteTest2 running  **********\n\n")
  783.  
  784.       print ("Writing files which are not open...\n")
  785.  
  786.       i = Sys_Write (0, &buffer[0], 30)
  787.       if i == -1
  788.         print ("Okay\n")
  789.       else
  790.         print ("*****  ERROR: return value from Sys_Write (")
  791.         printInt (i)
  792.         print (") is incorrect  *****\n")
  793.       endIf
  794.  
  795.       i = Sys_Write (1, &buffer[0], 30)
  796.       if i == -1
  797.         print ("Okay\n")
  798.       else
  799.         print ("*****  ERROR: return value from Sys_Write (")
  800.         printInt (i)
  801.         print (") is incorrect  *****\n")
  802.       endIf
  803.  
  804.       i = Sys_Write (9, &buffer[0], 30)
  805.       if i == -1
  806.         print ("Okay\n")
  807.       else
  808.         print ("*****  ERROR: return value from Sys_Write (")
  809.         printInt (i)
  810.         print (") is incorrect  *****\n")
  811.       endIf
  812.  
  813.       print ("Writing files with invalid file descriptors...\n")
  814.  
  815.       i = Sys_Write (-1, &buffer[0], 30)
  816.       if i == -1
  817.         print ("Okay\n")
  818.       else
  819.         print ("*****  ERROR: return value from Sys_Write (")
  820.         printInt (i)
  821.         print (") is incorrect  *****\n")
  822.       endIf
  823.  
  824.       i = Sys_Write (-12345, &buffer[0], 30)
  825.       if i == -1
  826.         print ("Okay\n")
  827.       else
  828.         print ("*****  ERROR: return value from Sys_Write (")
  829.         printInt (i)
  830.         print (") is incorrect  *****\n")
  831.       endIf
  832.  
  833.       i = Sys_Write (12345, &buffer[0], 30)
  834.       if i == -1
  835.         print ("Okay\n")
  836.       else
  837.         print ("*****  ERROR: return value from Sys_Write (")
  838.         printInt (i)
  839.         print (") is incorrect  *****\n")
  840.       endIf
  841.  
  842.       fd = Sys_Open ("file2")
  843.  
  844.       print ("Writing with negative sizeInBytes...\n")
  845.  
  846.       i = Sys_Write (0, &buffer[0], -1)
  847.       if i == -1
  848.         print ("Okay\n")
  849.       else
  850.         print ("*****  ERROR: return value from Sys_Write (")
  851.         printInt (i)
  852.         print (") is incorrect  *****\n")
  853.       endIf
  854.  
  855.       i = Sys_Write (0, &buffer[0], -123123123)
  856.       if i == -1
  857.         print ("Okay\n")
  858.       else
  859.         print ("*****  ERROR: return value from Sys_Write (")
  860.         printInt (i)
  861.         print (") is incorrect  *****\n")
  862.       endIf
  863.  
  864.       print ("Make sure file size is unchanged...\n")
  865.  
  866.       i = Sys_Seek (fd, -1)
  867.       if i == 21
  868.         print ("Okay\n")
  869.       else
  870.         print ("*****  ERROR: return value from Sys_Seek (")
  871.         printInt (i)
  872.         print (") is incorrect  *****\n")
  873.       endIf
  874.  
  875.       print ("\n**********  Test Complete  **********\n\n")
  876.  
  877.     endFunction
  878.  
  879. -----------------------------  WriteTest3  ---------------------------------
  880.  
  881.   function WriteTest3 ()
  882.     --
  883.     -- This function updates a file that is 3 sectors long.  It does 4
  884.     -- writes (some of which span sector boundaries) and then checks that
  885.     -- the data written is correct when read back.  It then repeats the
  886.     -- the 4 writes and checking, using different data.
  887.     --
  888.       var fd: int
  889.  
  890.       print ("\n**********  WriteTest3 running  **********\n\n")
  891.  
  892.       fd = Sys_Open ("file3")
  893.  
  894.       DoWrite   (fd, true, 0,     "abcdefghijklmnopqrstuvwxyz")
  895.       DoWrite   (fd, true, 8179,  "zyxwvutsrqponmlkjihgfedcba")
  896.       DoWrite   (fd, true, 16371, "aaaaabbbbbcccccdddddeeeeef")
  897.       DoWrite   (fd, true, 24550, "ggggghhhhhiiiiijjjjjkkkkkl")
  898.  
  899.       CheckFile (fd, true, 0,     "abcdefghijklmnopqrstuvwxyz")
  900.       CheckFile (fd, true, 8179,  "zyxwvutsrqponmlkjihgfedcba")
  901.       CheckFile (fd, true, 16371, "aaaaabbbbbcccccdddddeeeeef")
  902.       CheckFile (fd, true, 24550, "ggggghhhhhiiiiijjjjjkkkkkl")
  903.  
  904.       DoWrite   (fd, true, 16371, "AAAABBBBCCCCDDDDEEEEFFFFGG")
  905.       DoWrite   (fd, true, 0,     "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  906.       DoWrite   (fd, true, 24550, "HHHHIIIIJJJJKKKKLLLLMMMMNN")
  907.       DoWrite   (fd, true, 8179,  "ZYXWVUTSRQPONMLKJIHGFEDCBA")
  908.  
  909.       CheckFile (fd, true, 0,     "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  910.       CheckFile (fd, true, 8179,  "ZYXWVUTSRQPONMLKJIHGFEDCBA")
  911.       CheckFile (fd, true, 16371, "AAAABBBBCCCCDDDDEEEEFFFFGG")
  912.       CheckFile (fd, true, 24550, "HHHHIIIIJJJJKKKKLLLLMMMMNN")
  913.  
  914.       print ("\n**********  Test Complete  **********\n\n")
  915.  
  916.     endFunction
  917.  
  918. -------------------------------  DoWrite  -------------------------------
  919.  
  920.   function DoWrite (fd: int, wantPrinting: bool, positionInFile: int, data: String)
  921.     --
  922.     -- This function is passed a position in the file and the data to write.
  923.     -- It seeks to the position and writes the data.  It also checks
  924.     -- to make sure the syscalls return expected values.
  925.     --
  926.       var i: int
  927.       if wantPrinting
  928.         print ("Writing \"")
  929.         print (data)
  930.         print ("\" to position ")
  931.         printInt (positionInFile)
  932.         print (" in file...\n")
  933.       endIf
  934.       i = Sys_Seek (fd, positionInFile)
  935.       if i != positionInFile
  936.         print ("*****  ERROR: return value from Sys_Seek (")
  937.         printInt (i)
  938.         print (") is incorrect  *****\n")
  939.       endIf
  940.       i = Sys_Write (fd, & data[0], data arraySize)
  941.       if i == data arraySize
  942.         if wantPrinting
  943.           print ("Okay\n")
  944.         endIf
  945.       else
  946.         print ("*****  ERROR: return value from Sys_Write (")
  947.         printInt (i)
  948.         print (") is incorrect  *****\n")
  949.       endIf
  950.     endFunction
  951.  
  952. -------------------------------  CheckFile  -------------------------------
  953.  
  954.   var checkBuffer: array [10000] of char = new array of char { 10000 of '?' }
  955.  
  956.   function CheckFile (fd: int, wantPrinting: bool, positionInFile: int, data: String)
  957.     --
  958.     -- This function is passed a position in the file and the data that
  959.     -- we expect to find there.  It seeks to that position, reads from the file
  960.     -- and checks to make sure the data we read was what was expected.
  961.     --
  962.     -- This function is not reentrant, since it uses a global buffer.
  963.     --
  964.       var i: int
  965.       if checkBuffer arraySize < data arraySize
  966.         print ("LOGIC ERROR: data should not be larger than our buffer size\n")
  967.       endIf
  968.       if wantPrinting
  969.         print ("Checking that the file contains \"")
  970.         print (data)
  971.         print ("\" at position ")
  972.         printInt (positionInFile)
  973.         print ("...\n")
  974.       endIf
  975.       i = Sys_Seek (fd, positionInFile)
  976.       if i != positionInFile
  977.         print ("*****  ERROR: return value from Sys_Seek (")
  978.         printInt (i)
  979.         print (") is incorrect  *****\n")
  980.       endIf
  981.       i = Sys_Read (fd, &checkBuffer[0], data arraySize)
  982.       if i != data arraySize
  983.         print ("*****  ERROR: return value from Sys_Read (")
  984.         printInt (i)
  985.         print (") is incorrect  *****\n")
  986.       endIf
  987.       if MemoryEqual (&checkBuffer[0], &data[0], data arraySize)
  988.         if wantPrinting
  989.           print ("Okay.\n")
  990.         endIf
  991.       else
  992.         print ("*****  ERROR: The data read (\"")
  993.         for i = 0 to data arraySize - 1
  994.           printChar (checkBuffer[i])
  995.         endFor
  996.         print ("\") is incorrect  *****\n")
  997.       endIf
  998.  
  999.     endFunction
  1000.  
  1001. -----------------------------  WriteTest4  ---------------------------------
  1002.  
  1003.   function WriteTest4 ()
  1004.     --
  1005.     -- This function updates a file that is 3 sectors long.  First it writes
  1006.     -- some data in the file and checks to make sure it was written correctly.
  1007.     -- Then it forks off a child and waits for it.  The child will write to the
  1008.     -- file at the current position.  Then the parent will fork off another child
  1009.     -- which will write some more to the file.  After the second child finishes,
  1010.     -- the parent will read the file to make sure it is correct.
  1011.     --
  1012.       var fd, i, pid: int
  1013.  
  1014.       print ("\n**********  WriteTest4 running  **********\n\n")
  1015.  
  1016.       fd = Sys_Open ("file3")
  1017.  
  1018.       -- Initialize the file, which may contain stuff from previous runs...
  1019.       DoWrite   (fd, true, 0,     "abcdefghijklmnopqrstuvwxyz")
  1020.       CheckFile (fd, true, 0,     "abcdefghijklmnopqrstuvwxyz")
  1021.  
  1022.       i = Sys_Seek (fd, 3)
  1023.  
  1024.       -- Fork off a child to write some characters...
  1025.       pid = Sys_Fork ()
  1026.       if pid == 0
  1027.         DoWrite2 (fd, "0123")
  1028.         Sys_Exit (0)
  1029.       else
  1030.         i = Sys_Join (pid)
  1031.       endIf
  1032.  
  1033.       DoWrite2 (fd, "ABC")
  1034.  
  1035.       -- Fork off a child to write some characters...
  1036.       pid = Sys_Fork ()
  1037.       if pid == 0
  1038.         DoWrite2 (fd, "4567")
  1039.         Sys_Exit (0)
  1040.       else
  1041.         i = Sys_Join (pid)
  1042.       endIf
  1043.  
  1044.       DoWrite2 (fd, "DEF")
  1045.  
  1046.       -- Fork off a child to write some characters...
  1047.       pid = Sys_Fork ()
  1048.       if pid == 0
  1049.         DoWrite2 (fd, "89")
  1050.         Sys_Exit (0)
  1051.       else
  1052.         i = Sys_Join (pid)
  1053.       endIf
  1054.  
  1055.       DoWrite2 (fd, "GHI")
  1056.  
  1057.       -- Check that the file contains the right stuff...
  1058.       CheckFile (fd, true, 0, "abc0123ABC4567DEF89GHIwxyz")
  1059.  
  1060.       print ("\n**********  Test Complete  **********\n\n")
  1061.  
  1062.     endFunction
  1063.  
  1064. -----------------------------  DoWrite2  ---------------------------------
  1065.  
  1066.   function DoWrite2 (fd: int, data: String)
  1067.     --
  1068.     -- This function is passed some data which it writes at the current position.
  1069.     --
  1070.       var i: int
  1071.       print ("Writing \"")
  1072.       print (data)
  1073.       print ("\" to current position...\n")
  1074.       i = Sys_Write (fd, & data[0], data arraySize)
  1075.       if i == data arraySize
  1076.         print ("Okay\n")
  1077.       else
  1078.         print ("*****  ERROR: return value from Sys_Write (")
  1079.         printInt (i)
  1080.         print (") is incorrect  *****\n")
  1081.       endIf
  1082.  
  1083.     endFunction
  1084.  
  1085. -----------------------------  WriteTest5  ---------------------------------
  1086.  
  1087.   function WriteTest5 ()
  1088.     --
  1089.     -- This function updates a file that is 3 sectors long.  First it writes
  1090.     -- some data in the file and checks to make sure it was written correctly.
  1091.     -- Then it forks off several children.  Each child will open the file
  1092.     -- independently and will do lots of writes to different sectors.
  1093.     -- The parent will read the file repeatedly, until it sees that all children
  1094.     -- have written their last character.  Then the parent will check that the
  1095.     -- file contents are correct.
  1096.     --
  1097.       var fd, pid1, pid2, i: int
  1098.  
  1099.       print ("\n**********  WriteTest5 running  **********\n\n")
  1100.  
  1101.       fd = Sys_Open ("file3")
  1102.  
  1103.       -- Initialize the file, which may contain stuff from previous runs...
  1104.       DoWrite   (fd, true, 0,     "abcdefghijklmnopqrstuvwxyz")
  1105.       CheckFile (fd, true, 0,     "abcdefghijklmnopqrstuvwxyz")
  1106.       DoWrite   (fd, true, 8192,  "abcdefghijklmnopqrstuvwxyz")
  1107.       CheckFile (fd, true, 8192,  "abcdefghijklmnopqrstuvwxyz")
  1108.       Sys_Close (fd)
  1109.  
  1110.       -- Fork off the first child to write some characters.
  1111.       -- All of these writes are to even numbered positions.
  1112.       print ("Make sure that the activity of child 1 and child 2 is interleaved.\n")
  1113.       pid1 = Sys_Fork ()
  1114.       if pid1 == 0
  1115.         print ("......Child 1 beginning......\n")
  1116.         -- Wait for the forking of child 2 to finish.
  1117.         for i = 1 to 140
  1118.           Sys_Yield ()
  1119.         endFor
  1120.         fd = Sys_Open ("file3")
  1121.         DoWrite (fd, true, 0, "A")
  1122.         DoWrite (fd, true, 8204, "M")
  1123.         Sys_Yield ()
  1124.         Sys_Yield ()
  1125.         Sys_Yield ()
  1126.         Sys_Yield ()
  1127.         Sys_Yield ()
  1128.         Sys_Yield ()
  1129.         Sys_Yield ()
  1130.         DoWrite (fd, true, 2, "C")
  1131.         DoWrite (fd, true, 8206, "O")
  1132.         Sys_Yield ()
  1133.         Sys_Yield ()
  1134.         Sys_Yield ()
  1135.         DoWrite (fd, true, 4, "E")
  1136.         DoWrite (fd, true, 8208, "Q")
  1137.         DoWrite (fd, true, 6, "G")
  1138.         Sys_Yield ()
  1139.         Sys_Yield ()
  1140.         DoWrite (fd, true, 8, "I")
  1141.         DoWrite (fd, true, 10, "K")
  1142.         Sys_Yield ()
  1143.         Sys_Yield ()
  1144.         Sys_Yield ()
  1145.         Sys_Yield ()
  1146.         Sys_Yield ()
  1147.         DoWrite (fd, true, 8210, "S")
  1148.         DoWrite (fd, true, 8212, "U")
  1149.         DoWrite (fd, true, 8214, "W")
  1150.         print ("......Child 1 ending......\n")
  1151.         Sys_Exit (0)
  1152.       endIf
  1153.  
  1154.       -- Fork off second child to write some other characters.
  1155.       -- All of these writes are to odd numbered positions.
  1156.       pid2 = Sys_Fork ()
  1157.       if pid2 == 0
  1158.         print ("......Child 2 beginning......\n")
  1159.         fd = Sys_Open ("file3")
  1160.         DoWrite (fd, true, 1, "B")
  1161.         Sys_Yield ()
  1162.         Sys_Yield ()
  1163.         Sys_Yield ()
  1164.         DoWrite (fd, true, 8205, "N")
  1165.         DoWrite (fd, true, 3, "D")
  1166.         DoWrite (fd, true, 8207, "P")
  1167.         Sys_Yield ()
  1168.         Sys_Yield ()
  1169.         DoWrite (fd, true, 5, "F")
  1170.         DoWrite (fd, true, 8209, "R")
  1171.         DoWrite (fd, true, 8211, "T")
  1172.         Sys_Yield ()
  1173.         Sys_Yield ()
  1174.         DoWrite (fd, true, 8213, "V")
  1175.         DoWrite (fd, true, 7, "H")
  1176.         DoWrite (fd, true, 9, "J")
  1177.         Sys_Yield ()
  1178.         Sys_Yield ()
  1179.         Sys_Yield ()
  1180.         DoWrite (fd, true, 11, "L")
  1181.         DoWrite (fd, true, 8215, "X")
  1182.         print ("......Child 2 ending......\n")
  1183.         Sys_Exit (0)
  1184.       endIf
  1185.  
  1186.       -- Wait for both children...
  1187.       i = Sys_Join (pid1)
  1188.       i = Sys_Join (pid2)
  1189.  
  1190.       -- Now check the file's contents...
  1191.       fd = Sys_Open ("file3")
  1192.       CheckFile (fd, true, 0,     "ABCDEFGHIJKLmnopqrstuvwxyz")
  1193.       CheckFile (fd, true, 8192,  "abcdefghijklMNOPQRSTUVWXyz")
  1194.  
  1195.       print ("\n**********  Test Complete  **********\n\n")
  1196.  
  1197.     endFunction
  1198.  
  1199. -----------------------------  WriteTest6  ---------------------------------
  1200.  
  1201.   var arr1: array [10000] of char = new array of char { 10000 of '?' }
  1202.  
  1203.   function WriteTest6 ()
  1204.     --
  1205.     -- This function updates a file that is 3 sectors long.  It does 2
  1206.     -- writes (which span sector boundaries) and then checks that
  1207.     -- the data written is correct when read back.  It then repeats the
  1208.     -- the 2 writes and checking, using different data.
  1209.     --
  1210.       var fd: int
  1211.  
  1212.       print ("\n**********  WriteTest6 running  **********\n\n")
  1213.  
  1214.       fd = Sys_Open ("file3")
  1215.  
  1216.       print ("Initializing data array to ABCDEFGHIJKLMNOPQRSTUVWXYZ...\n")
  1217.       FillArray (& arr1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  1218.  
  1219.       print ("Writing 10000 bytes to pos 0...\n")
  1220.       DoWrite (fd, false, 0, &arr1)
  1221.  
  1222.       print ("Initializing data array to abcdefghijklmnopqrstuvwxyz...\n")
  1223.       FillArray (& arr1, "abcdefghijklmnopqrstuvwxyz")
  1224.  
  1225.       print ("Writing 10000 bytes to pos 10000...\n")
  1226.       DoWrite (fd, false, 10000, &arr1)
  1227.  
  1228.       CheckFile (fd, true, 0, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  1229.       CheckFile (fd, true, 9958, "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPabcdefghijklmnopqrstuvwxyz")
  1230.       CheckFile (fd, true, 19958, "abcdefghijklmnopqrstuvwxyzabcdefghijklmnop")
  1231.       CheckFile (fd, true, 3900, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  1232.       CheckFile (fd, true, 13900, "abcdefghijklmnopqrstuvwxyz")
  1233.  
  1234.       print ("Initializing data array to ABCDEFGHIJKLMNOPQRSTUVWXYZ...\n")
  1235.       FillArray (& arr1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  1236.  
  1237.       print ("Checking 10000 bytes at pos 0...\n")
  1238.       CheckFile (fd, false, 0, &arr1)
  1239.  
  1240.       print ("Initializing data array to abcdefghijklmnopqrstuvwxyz...\n")
  1241.       FillArray (& arr1, "abcdefghijklmnopqrstuvwxyz")
  1242.  
  1243.       print ("Checking 10000 bytes at pos 10000...\n")
  1244.       CheckFile (fd, false, 10000, &arr1)
  1245.  
  1246.       -- Now do it all again with different data
  1247.  
  1248.       print ("Initializing data array to ZYXWVUTSRQPONMLKJIHGFEDCBA...\n")
  1249.       FillArray (& arr1, "ZYXWVUTSRQPONMLKJIHGFEDCBA")
  1250.  
  1251.       print ("Writing 10000 bytes to pos 0...\n")
  1252.       DoWrite (fd, false, 0, &arr1)
  1253.  
  1254.       print ("Initializing data array to zyxwvutsrqponmlkjihgfedcba...\n")
  1255.       FillArray (& arr1, "zyxwvutsrqponmlkjihgfedcba")
  1256.  
  1257.       print ("Writing 10000 bytes to pos 10000...\n")
  1258.       DoWrite (fd, false, 10000, &arr1)
  1259.  
  1260.       CheckFile (fd, true, 0, "ZYXWVUTSRQPONMLKJIHGFEDCBA")
  1261.       CheckFile (fd, true, 9958,
  1262.          "ZYXWVUTSRQPONMLKJIHGFEDCBAZYXWVUTSRQPONMLKzyxwvutsrqponmlkjihgfedcba")
  1263.       CheckFile (fd, true, 19958, "zyxwvutsrqponmlkjihgfedcbazyxwvutsrqponmlk")
  1264.       CheckFile (fd, true, 3900, "ZYXWVUTSRQPONMLKJIHGFEDCBA")
  1265.       CheckFile (fd, true, 13900, "zyxwvutsrqponmlkjihgfedcba")
  1266.  
  1267.       print ("Initializing data array to ZYXWVUTSRQPONMLKJIHGFEDCBA...\n")
  1268.       FillArray (& arr1, "ZYXWVUTSRQPONMLKJIHGFEDCBA")
  1269.  
  1270.       print ("Checking 10000 bytes at pos 0...\n")
  1271.       CheckFile (fd, false, 0, &arr1)
  1272.  
  1273.       print ("Initializing data array to zyxwvutsrqponmlkjihgfedcba...\n")
  1274.       FillArray (& arr1, "zyxwvutsrqponmlkjihgfedcba")
  1275.  
  1276.       print ("Checking 10000 bytes at pos 10000...\n")
  1277.       CheckFile (fd, false, 10000, &arr1)
  1278.  
  1279.       print ("\n**********  Test Complete  **********\n\n")
  1280.  
  1281.     endFunction
  1282.  
  1283. -----------------------------  FillArray  ---------------------------------
  1284.  
  1285.   function FillArray (a: ptr to array of char, s: String)
  1286.     --
  1287.     -- This routine fills the array "a" with repeated copies of the
  1288.     -- string "s".
  1289.     --
  1290.       var toPtr: int = (&a[0]) asInteger
  1291.           fromPtr: int = (&s[0]) asInteger
  1292.           lastFullCopy, remaining: int
  1293.           sizeOfS: int = s arraySize
  1294.         lastFullCopy = (&a[a arraySize - 1]) asInteger - sizeOfS + 1
  1295.         while toPtr <= lastFullCopy
  1296.           MemoryCopy (toPtr, fromPtr, sizeOfS)
  1297.           toPtr = toPtr + sizeOfS
  1298.         endWhile
  1299.         remaining = (&a[a arraySize - 1]) asInteger - toPtr + 1
  1300.         if remaining > 0
  1301.           MemoryCopy (toPtr, fromPtr, remaining)
  1302.         endIf
  1303.  
  1304.     endFunction
  1305.  
  1306. -----------------------------  OpenTest3  ---------------------------------
  1307.  
  1308.   function OpenTest3 ()
  1309.     --
  1310.     -- Test the Open syscall with a long file name.  The file exists, but
  1311.     -- this should fail since the name is too long to move into a kernel buffer.
  1312.     -- Then try an Open using a filename that happens to cross a page boundary.
  1313.     --
  1314.       var fd: int
  1315.           p: int
  1316.           name: String = "file1234abcd"
  1317.       print ("\n**********  OpenTest3 running  **********\n\n")
  1318.  
  1319.       print ("Attempting to open an existing file whose name exceeds the maximum.\n")
  1320.       fd = Sys_Open ("FileWithVeryLongName012345678901234567890123456789")
  1321.       Check (fd, -1)
  1322.       if fd == -1
  1323.         print ("Okay.\n")
  1324.       else
  1325.         print ("*****  ERROR: Open returned an unexpected value ( ")
  1326.         printInt (fd)
  1327.         print (")  *****\n")
  1328.       endIf
  1329.  
  1330.       -- Next find a page boundary within arr1...
  1331.       p = (& arr1[9999]) asInteger
  1332.       p = (p & 0xffffe000) - 8
  1333.       MemoryCopy (p, name asInteger, name arraySize+4)    -- Copy string plus length
  1334.  
  1335.       print ("Attempting to open an existing file using a string which crosses a page boundary.\n")
  1336.       fd = Sys_Open (p asPtrTo array of char)
  1337.       if fd != -1
  1338.         print ("Okay.\n")
  1339.       else
  1340.         print ("*****  ERROR: Open returned a an unexpected value ( ")
  1341.         printInt (fd)
  1342.         print (")  *****\n")
  1343.       endIf
  1344.  
  1345.       print ("\n**********  Test Complete  **********\n\n")
  1346.  
  1347.     endFunction
  1348.  
  1349. -----------------------------  ExecTest1  ---------------------------------
  1350.  
  1351.   function ExecTest1 ()
  1352.     --
  1353.     -- This is a basic test of the Exec syscall.
  1354.     --
  1355.       var i: int
  1356.  
  1357.       print ("\n**********  ExecTest1 running  **********\n\n")
  1358.  
  1359.       print ("Should print 'Hello, world'...\n")
  1360.       i = Sys_Exec ("Program1")
  1361.       print ("*****  ERROR: Exec returned an unexpected value ( ")
  1362.       printInt (i)
  1363.       print (")  *****\n")
  1364.  
  1365.     endFunction
  1366.  
  1367. -----------------------------  ExecTest2  ---------------------------------
  1368.  
  1369.   function ExecTest2 ()
  1370.     --
  1371.     -- This is a basic test of the Exec syscall.
  1372.     --
  1373.       var i: int
  1374.           p: int
  1375.           name: String = "Program1"
  1376.  
  1377.       print ("\n**********  ExecTest2 running  **********\n\n")
  1378.  
  1379.       print ("Attempting to exec an existing file whose name exceeds the maximum.\n")
  1380.       i = Sys_Exec ("FileWithVeryLongName012345678901234567890123456789")
  1381.       Check (i, -1)
  1382.  
  1383.       print ("Attempting to exec an existing file which is not an executable.\n")
  1384.       i = Sys_Exec ("file1")
  1385.       Check (i, -1)
  1386.  
  1387.       -- Next find a page boundary within arr1...
  1388.       p = (& arr1[9999]) asInteger
  1389.       p = (p & 0xffffe000) - 8
  1390.       MemoryCopy (p, name asInteger, name arraySize+4)    -- Copy string plus length
  1391.  
  1392.       print ("Attempting to open an existing file using a string which crosses a page boundary.\n")
  1393.       print ("Should print 'Hello, world'...\n")
  1394.       i = Sys_Exec (p asPtrTo array of char)
  1395.       print ("*****  ERROR: Exec returned an unexpected value ( ")
  1396.       printInt (i)
  1397.       print (")  *****\n")
  1398.  
  1399.     endFunction
  1400.  
  1401. -----------------------------  ExecTest3  ---------------------------------
  1402.  
  1403.   function ExecTest3 ()
  1404.     --
  1405.     -- This is a basic test of the Exec syscall.
  1406.     --
  1407.       var fd, i, counter: int
  1408.  
  1409.       print ("\n**********  ExecTest3 running  **********\n\n")
  1410.       fd = Sys_Open ("file3")
  1411.       if fd != 0
  1412.         print ("*****  ERROR: Sys_Open returned an unexpected value ( ")
  1413.         printInt (i)
  1414.         print (")  *****\n")
  1415.       endIf
  1416.  
  1417.       counter = 1
  1418.       i = Sys_Write (fd, (&counter) asPtrTo char, 4)
  1419.  
  1420.       print ("Should print 'Hello, world' 20 times...\n")
  1421.       i = Sys_Exec ("Program2")
  1422.       print ("*****  ERROR: Exec returned an unexpected value ( ")
  1423.       printInt (i)
  1424.       print (")  *****\n")
  1425.  
  1426.     endFunction
  1427.  
  1428.  
  1429. -----------------------------  Check  ---------------------------------
  1430.  
  1431.   function Check (i, expectedVal: int)
  1432.     --
  1433.     -- This function is passed the return code from a syscall and
  1434.     -- the expected value.  It prints an error message if it is incorrect.
  1435.     --
  1436.       if i == expectedVal
  1437.         print ("Okay.\n")
  1438.       else
  1439.         print ("*****  ERROR: Return value from syscall (")
  1440.         printInt (i)
  1441.         print (") is incorrect  *****\n")
  1442.       endIf
  1443.       
  1444.     endFunction
  1445.  
  1446.  
  1447.  
  1448. endCode
  1449.