home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / vb_code2 / sorts / testsort.bas < prev   
BASIC Source File  |  1992-09-23  |  3KB  |  102 lines

  1.  
  2. '   This is the test case for SortStrings()
  3. '
  4. '   The test output will show up in the immediate
  5. '   panel.
  6.  
  7. '   The test program generates an array of strings
  8. '   filled with short strings of random characters
  9. '   in the range "A" to "Z", in both upper case and
  10. '   lower case. It then sorts them in ascending and
  11. '   descending order, first respecting the letters'
  12. '   case then ignoring it (sorting lexically).
  13.  
  14. '   Remember that upper case letters A-Z all appear
  15. '   before the lower case letters, so if you respect
  16. '   the case of the strings, "Z" sorts before "a".
  17.  
  18.  
  19. Sub TestSorts ()
  20. '
  21. '   Runs the test case for SortStrings()
  22. '
  23.     Const ARRAYSIZE = 5         'ubound of the test array
  24.     Const LOWINDEX  = 0         'first array index for sort
  25.     Const HIGHINDEX = ARRAYSIZE 'last array index for sort
  26.  
  27.     Dim firstCode As Integer
  28.     Dim lastCode  As Integer
  29.     Dim thisChar  As String
  30.     ReDim a(ARRAYSIZE) As String
  31.  
  32.     Randomize Timer         'seed the random number generator
  33.  
  34.     firstCode = Asc("A")
  35.     lastCode = Asc("Z")
  36.  
  37.     For i = LOWINDEX To HIGHINDEX
  38.         thisChar = Chr$(TestSortRandomInt(firstCode, lastCode))
  39.         If TestSortRandomInt(0, 1) = 1 Then     'about half the time,
  40.             thisChar = LCase$(thisChar)         '   change case
  41.         End If
  42.         a(i) = String$(8, thisChar)             'make a short string of char
  43.     Next i
  44.  
  45.     Debug.Print ""
  46.     Debug.Print "Initially:"
  47.     For i = LOWINDEX To HIGHINDEX
  48.         Debug.Print a(i)
  49.     Next i
  50.     Debug.Print ""
  51.  
  52.     Debug.Print "Ascending sort:"
  53.     Debug.Print ""
  54.  
  55.     SortStrings a(), LOWINDEX, HIGHINDEX, SORTASCENDING
  56.  
  57.     Debug.Print "Respecting case:"
  58.     For i = LOWINDEX To HIGHINDEX
  59.         Debug.Print a(i)
  60.     Next i
  61.     Debug.Print ""
  62.  
  63.     SortStrings a(), LOWINDEX, HIGHINDEX, SORTASCENDING Or SORTIGNORECASE
  64.  
  65.     Debug.Print "Ignoring case:"
  66.     For i = LOWINDEX To HIGHINDEX
  67.         Debug.Print a(i)
  68.     Next i
  69.     Debug.Print ""
  70.  
  71.     Debug.Print ""
  72.     Debug.Print "Descending sort:"
  73.     Debug.Print ""
  74.  
  75.     SortStrings a(), LOWINDEX, HIGHINDEX, SORTDESCENDING
  76.  
  77.     Debug.Print "Respecting case:"
  78.     For i = LOWINDEX To HIGHINDEX
  79.         Debug.Print a(i)
  80.     Next i
  81.     Debug.Print ""
  82.  
  83.     SortStrings a(), LOWINDEX, HIGHINDEX, SORTDESCENDING Or SORTIGNORECASE
  84.  
  85.     Debug.Print "Ignoring case:"
  86.     For i = LOWINDEX To HIGHINDEX
  87.         Debug.Print a(i)
  88.     Next i
  89.     Debug.Print ""
  90.  
  91. End Sub
  92.  
  93. Function TestSortRandomInt (ByVal lowerBound As Integer, ByVal upperBound As Integer) As Integer
  94. '
  95. '   Returns a random integer in specified range.
  96. '   The range includes the end points.
  97. '
  98.    TestSortRandomInt = Int((upperBound - lowerBound + 1) * Rnd + lowerBound)
  99.  
  100. End Function
  101.  
  102.