Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
BlitzMax 15.89 KB | None | 0 0
  1.  
  2. Strict
  3.  
  4. #ANDROID_APP_LABEL="DataTypes Test"
  5. #HTML5_TRANSPILE_KILL_SWITCH_ENABLED=True
  6.  
  7. Import examplebaseapp
  8. #IF CERBERUSX
  9.     Import cerberus.stack
  10. #Else
  11.     Import monkey.stack
  12. #Endif
  13.  
  14. #IF TARGET = "html5"
  15.     Const TARGET_NAME := "HTML5"
  16. #Elseif TARGET = "ios"
  17.     Const TARGET_NAME := "iOS"
  18. #ElseIf TARGET = "android"
  19.     Const TARGET_NAME := "Android"
  20. #Else
  21.     Const TARGET_NAME := "Unknown Target"
  22. #Endif
  23.  
  24. Const CHUNK_SIZE := 128
  25.  
  26. Const INT_ITERATIONS := 1000000
  27. Const FLOAT_ITERATIONS := INT_ITERATIONS
  28. Const STRING_ITERATIONS := 1000
  29. Const INT_ARRAY_ITERATIONS := 10000
  30. Const FLOAT_ARRAY_ITERATIONS := INT_ARRAY_ITERATIONS
  31. Const STRING_ARRAY_ITERATIONS := INT_ARRAY_ITERATIONS
  32.  
  33. Const TESTS_MIN := 0
  34. Const TESTS_MAX := 8
  35.  
  36. Const TEST_ALL := -1
  37. Const TEST_INTS := 0
  38. Const TEST_FLOATS := 1
  39. Const TEST_STRINGS := 2
  40. Const TEST_INT_ARRAYS := 3
  41. Const TEST_FLOAT_ARRAYS := 4
  42. Const TEST_STRING_ARRAYS := 5
  43. Const TEST_CHUNKED_INT_ARRAYS := 6
  44. Const TEST_CHUNKED_FLOAT_ARRAYS := 7
  45. Const TEST_CHUNKED_STRING_ARRAYS := 8
  46.  
  47. Global TEST_LABELS := ["ints", "floats", "strings", "int arrays", "float arrays", "string arrays", "chunked int arrays", "chunked float arrays", "chunked string arrays"]
  48.  
  49. Function Main:Int()
  50.     New MyApp()
  51.     Return 0
  52. End
  53.  
  54. Class MyApp Extends ExampleBaseApp
  55.     Field sessionActive := False
  56.     Field sessionCount:Int
  57.     Field sessionWhich:Int
  58.     Field sessionResults:StringStack
  59.     Field sessionIterations:Int
  60.    
  61.     Field currentTest:Int
  62.     Field currentTestStarted := False
  63.     Field currentTestIteration:int
  64.     Field currentTestCount:Int
  65.    
  66.     Method OnExampleCreate:Void()
  67.         AddButton("runTestAllX1", "Test All x1", "33%")
  68.         AddButton("runTestAllX25", "Test All x25", "33%")
  69.         AddButton("runTestAllX100", "Test All x100", "fill")
  70.        
  71.         AddButton("runTestIntsX1", "Test Ints x1", "33%")
  72.         AddButton("runTestIntsX25", "Test Ints x25", "33%")
  73.         AddButton("runTestIntsX100", "Test Ints x100", "fill")
  74.        
  75.         AddButton("runTestFloatsX1", "Test Floats x1", "33%")
  76.         AddButton("runTestFloatsX25", "Test Floats x25", "33%")
  77.         AddButton("runTestFloatsX100", "Test Floats x100", "fill")
  78.        
  79.         AddButton("runTestStringsX1", "Test Strings x1", "33%")
  80.         AddButton("runTestStringsX25", "Test Strings x25", "33%")
  81.         AddButton("runTestStringsX100", "Test Strings x100", "fill")
  82.        
  83.         AddButton("runTestIntArraysX1", "Test Int Arrays x1", "33%")
  84.         AddButton("runTestIntArraysX25", "Test Int Arrays x25", "33%")
  85.         AddButton("runTestIntArraysX100", "Test Int Arrays x100", "fill")
  86.        
  87.         AddButton("runTestFloatArraysX1", "Test Float Arrays x1", "33%")
  88.         AddButton("runTestFloatArraysX25", "Test Float Arrays x25", "33%")
  89.         AddButton("runTestFloatArraysX100", "Test Float Arrays x100", "fill")
  90.        
  91.         AddButton("runTestStringArraysX1", "Test String Arrays x1", "33%")
  92.         AddButton("runTestStringArraysX25", "Test String Arrays x25", "33%")
  93.         AddButton("runTestStringArraysX100", "Test String Arrays x100", "fill")
  94.        
  95.         AddButton("runTestChunkedIntArraysX1", "Test Chunked Int Arrays x1", "33%")
  96.         AddButton("runTestChunkedIntArraysX25", "Test Chunked Int Arrays x25", "33%")
  97.         AddButton("runTestChunkedIntArraysX100", "Test Chunked Int Arrays x100", "fill")
  98.        
  99.         AddButton("runTestChunkedFloatArraysX1", "Test Chunked Float Arrays x1", "33%")
  100.         AddButton("runTestChunkedFloatArraysX25", "Test Chunked Float Arrays x25", "33%")
  101.         AddButton("runTestChunkedFloatArraysX100", "Test FChunked loat Arrays x100", "fill")
  102.        
  103.         AddButton("runTestChunkedStringArraysX1", "Test Chunked String Arrays x1", "33%")
  104.         AddButton("runTestChunkedStringArraysX25", "Test Chunked String Arrays x25", "33%")
  105.         AddButton("runTestChunkedStringArraysX100", "Test Chunked String Arrays x100", "fill")
  106.     End
  107.    
  108.     Method OnExampleUpdate:Void()
  109.         If sessionActive
  110.             UpdateTests()
  111.         Endif
  112.     End
  113.    
  114.     Method OnButtonPressed:Void(id:String, button:Button)
  115.         Select id
  116.             Case "runTestAllX1"
  117.                 StartTests(TEST_ALL, 1)
  118.             Case "runTestAllX25"
  119.                 StartTests(TEST_ALL, 25)
  120.             Case "runTestAllX100"
  121.                 StartTests(TEST_ALL, 100)
  122.                
  123.             Case "runTestIntsX1"
  124.                 StartTests(TEST_INTS, 1)
  125.             Case "runTestIntsX25"
  126.                 StartTests(TEST_INTS, 25)
  127.             Case "runTestIntsX100"
  128.                 StartTests(TEST_INTS, 100)
  129.                
  130.             Case "runTestFloatsX1"
  131.                 StartTests(TEST_FLOATS, 1)
  132.             Case "runTestFloatsX25"
  133.                 StartTests(TEST_FLOATS, 25)
  134.             Case "runTestFloatsX100"
  135.                 StartTests(TEST_FLOATS, 100)
  136.                
  137.             Case "runTestStringsX1"
  138.                 StartTests(TEST_STRINGS, 1)
  139.             Case "runTestStringsX25"
  140.                 StartTests(TEST_STRINGS, 25)
  141.             Case "runTestStringsX100"
  142.                 StartTests(TEST_STRINGS, 100)
  143.                
  144.             Case "runTestIntArraysX1"
  145.                 StartTests(TEST_INT_ARRAYS, 1)
  146.             Case "runTestIntArraysX25"
  147.                 StartTests(TEST_INT_ARRAYS, 25)
  148.             Case "runTestIntArraysX100"
  149.                 StartTests(TEST_INT_ARRAYS, 100)
  150.                
  151.             Case "runTestFloatArraysX1"
  152.                 StartTests(TEST_FLOAT_ARRAYS, 1)
  153.             Case "runTestFloatArraysX25"
  154.                 StartTests(TEST_FLOAT_ARRAYS, 25)
  155.             Case "runTestFloatArraysX100"
  156.                 StartTests(TEST_FLOAT_ARRAYS, 100)
  157.                
  158.             Case "runTestStringArraysX1"
  159.                 StartTests(TEST_STRING_ARRAYS, 1)
  160.             Case "runTestStringArraysX25"
  161.                 StartTests(TEST_STRING_ARRAYS, 25)
  162.             Case "runTestStringArraysX100"
  163.                 StartTests(TEST_STRING_ARRAYS, 100)
  164.                
  165.             Case "runTestChunkedIntArraysX1"
  166.                 StartTests(TEST_CHUNKED_INT_ARRAYS, 1)
  167.             Case "runTestChunkedIntArraysX25"
  168.                 StartTests(TEST_CHUNKED_INT_ARRAYS, 25)
  169.             Case "runTestChunkedIntArraysX100"
  170.                 StartTests(TEST_CHUNKED_INT_ARRAYS, 100)
  171.                
  172.             Case "runTestChunkedFloatArraysX1"
  173.                 StartTests(TEST_CHUNKED_FLOAT_ARRAYS, 1)
  174.             Case "runTestChunkedFloatArraysX25"
  175.                 StartTests(TEST_CHUNKED_FLOAT_ARRAYS, 25)
  176.             Case "runTestChunkedFloatArraysX100"
  177.                 StartTests(TEST_CHUNKED_FLOAT_ARRAYS, 100)
  178.                
  179.             Case "runTestChunkedStringArraysX1"
  180.                 StartTests(TEST_CHUNKED_STRING_ARRAYS, 1)
  181.             Case "runTestChunkedStringArraysX25"
  182.                 StartTests(TEST_CHUNKED_STRING_ARRAYS, 25)
  183.             Case "runTestChunkedStringArraysX100"
  184.                 StartTests(TEST_CHUNKED_STRING_ARRAYS, 100)
  185.         End
  186.     End
  187.  
  188.     'internal
  189.     Method StartTests:Void(which:Int, iterations:Int)
  190.         if sessionActive
  191.             AddFail("test already active")
  192.         Else
  193.             AddSuccess("starting tests...")
  194.            
  195.             sessionActive = True
  196.             sessionWhich = which
  197.             sessionIterations = iterations
  198.             sessionResults = New StringStack()
  199.             sessionCount = 0
  200.            
  201.             currentTest = TESTS_MIN
  202.         Endif
  203.     End
  204.    
  205.     Method UpdateTests:Void()
  206.         'has the session ended?
  207.         If currentTest > TESTS_MAX
  208.             'session finished, show results
  209.             sessionActive = False
  210.             sessionResults.Push("~ttotal average "+Ceil(Float(sessionCount) / sessionIterations)+" ms")
  211.            
  212.             #If CERBERUSX
  213.                 AddSuccess(TARGET_NAME+" Treebeard Results:")
  214.             #Else
  215.                 AddSuccess(TARGET_NAME+" Monkeymaster Results:")
  216.             #Endif
  217.            
  218.             For Local result := Eachin sessionResults
  219.                 AddSuccess(result)
  220.             Next
  221.         Else
  222.             'session still active
  223.             'can we run this test or is it ignored?
  224.             If currentTest = sessionWhich Or sessionWhich = TEST_ALL
  225.                 If Not currentTestStarted
  226.                     'test not started yet!
  227.                     currentTestStarted = True
  228.                     currentTestIteration = 1
  229.                     currentTestCount = 0
  230.                     AddSuccess("running test '"+TEST_LABELS[currentTest]+"' for "+sessionIterations+" iterations...")
  231.                 Else
  232.                     'has current test  ended?
  233.                     if currentTestIteration > sessionIterations
  234.                         'current test finished
  235.                         sessionResults.Push("~ttest '"+TEST_LABELS[currentTest]+"' average "+Ceil(Float(currentTestCount) / sessionIterations)+" ms")
  236.                        
  237.                         'next test
  238.                         currentTestStarted = False
  239.                         currentTest += 1
  240.                     Else
  241.                         'current test still running
  242.                         AddInfo("#"+currentTestIteration+"")
  243.  
  244.                         Local timestamp := Millisecs()
  245.                         Local operations:= 0
  246.                        
  247.                         Select currentTest
  248.                             Case TEST_INTS
  249.                                 operations = TestInts()
  250.                                
  251.                             Case TEST_FLOATS
  252.                                 operations = TestFloats()
  253.                                
  254.                             Case TEST_STRINGS
  255.                                 operations = TestStrings()
  256.                                
  257.                             Case TEST_INT_ARRAYS
  258.                                 operations = TestIntArrays()
  259.                                
  260.                             Case TEST_FLOAT_ARRAYS
  261.                                 operations = TestFloatArrays()
  262.                                
  263.                             Case TEST_STRING_ARRAYS
  264.                                 operations = TestStringArrays()
  265.                                
  266.                             Case TEST_CHUNKED_INT_ARRAYS
  267.                                 operations = TestChunkedIntArrays()
  268.                                
  269.                             Case TEST_CHUNKED_FLOAT_ARRAYS
  270.                                 operations = TestChunkedFloatArrays()
  271.                                
  272.                             Case TEST_CHUNKED_STRING_ARRAYS
  273.                                 operations = TestChunkedStringArrays()
  274.                         End
  275.                        
  276.                         Local took := Millisecs() - timestamp
  277.                         AddInfo("executed '"+TEST_LABELS[currentTest]+"' "+operations+" operations, took "+took+" ms")
  278.  
  279.                         currentTestCount += took
  280.                         sessionCount += took
  281.                        
  282.                         'next iteration
  283.                         currentTestIteration += 1
  284.                     Endif
  285.                 Endif
  286.             Endif
  287.         Endif
  288.     End
  289.  
  290.     Method TestInts:Int()
  291.         Local int1 := 0
  292.         Local iterations := INT_ITERATIONS
  293.         Local count := 0
  294.        
  295.         int1 = 0
  296.         For Local index := 0 Until iterations
  297.             int1 += 2
  298.             count += 1
  299.             if int1 > 1000
  300.                 int1 = int1+1
  301.             Endif
  302.         Next
  303.        
  304.         int1 = 0
  305.         For Local index := 0 Until iterations
  306.             int1 -= 2
  307.             count += 1
  308.             if int1 > 1000
  309.                 int1 = int1+1
  310.             Endif
  311.         Next
  312.        
  313.         int1 = 1
  314.         For Local index := 0 Until iterations
  315.             int1 *= 2
  316.             count += 1
  317.             if int1 > 1000
  318.                 int1 = int1+1
  319.             Endif
  320.         Next
  321.        
  322.         int1 = 1000000
  323.         For Local index := 0 Until iterations
  324.             int1 /= 2
  325.             count += 1
  326.             if int1 > 1000
  327.                 int1 = int1+1
  328.             Endif
  329.         Next
  330.        
  331.         return count
  332.     End
  333.    
  334.     Method TestFloats:Int()
  335.         Local float1 := 0.0
  336.         Local iterations := FLOAT_ITERATIONS
  337.         Local count := 0
  338.        
  339.         float1 = 0.0
  340.         For Local index := 0 Until iterations
  341.             float1 += 2.0
  342.             count += 1
  343.             if float1 > 1000.0
  344.                 float1 = float1+1
  345.             Endif
  346.         Next
  347.        
  348.         float1 = 0.0
  349.         For Local index := 0 Until iterations
  350.             float1 -= 2.0
  351.             count += 1
  352.             if float1 > 1000.0
  353.                 float1 = float1+1
  354.             Endif
  355.         Next
  356.        
  357.         float1 = 1.0
  358.         For Local index := 0 Until iterations
  359.             float1 *= 2.0
  360.             count += 1
  361.             if float1 > 1000.0
  362.                 float1 = float1+1
  363.             Endif
  364.         Next
  365.        
  366.         float1 = 1000000.0
  367.         For Local index := 0 Until iterations
  368.             float1 /= 2.0
  369.             count += 1
  370.             if float1 > 1000.0
  371.                 float1 = float1+1
  372.             Endif
  373.         Next
  374.        
  375.         return count
  376.     End
  377.    
  378.     Method TestStrings:Int()
  379.         Local string1 := ""
  380.         Local iterations := STRING_ITERATIONS
  381.         Local count := 0
  382.        
  383.         string1 = ""
  384.         For Local index := 0 Until iterations
  385.             string1 = string1+"APPEND"
  386.             count += 1
  387.         Next
  388.        
  389.         return count
  390.     End
  391.    
  392.     Method TestIntArrays:Int()
  393.         Local array1:Int[0]
  394.         Local iterations := INT_ARRAY_ITERATIONS
  395.         Local count := 0
  396.        
  397.         array1 = new Int[0]
  398.         For Local index := 0 Until iterations
  399.             if index = array1.Length
  400.                 array1 = array1.Resize(index+1)
  401.             Endif
  402.             array1[index] = count
  403.             count += 1
  404.         Next
  405.        
  406.         return count
  407.     End
  408.    
  409.     Method TestFloatArrays:Int()
  410.         Local array1:Float[0]
  411.         Local iterations := FLOAT_ARRAY_ITERATIONS
  412.         Local count := 0
  413.        
  414.         array1 = new Float[0]
  415.         For Local index := 0 Until iterations
  416.             if index = array1.Length
  417.                 array1 = array1.Resize(index+1)
  418.             Endif
  419.             array1[index] = Float(count)+0.345
  420.             count += 1
  421.         Next
  422.        
  423.         return count
  424.     End
  425.    
  426.     Method TestStringArrays:Int()
  427.         Local array1:String[0]
  428.         Local iterations := STRING_ARRAY_ITERATIONS
  429.         Local count := 0
  430.        
  431.         array1 = new String[0]
  432.         For Local index := 0 Until iterations
  433.             if index = array1.Length
  434.                 array1 = array1.Resize(index+1)
  435.             Endif
  436.             array1[index] = "value"+count
  437.             count += 1
  438.         Next
  439.        
  440.         return count
  441.     End
  442.    
  443.     Method TestChunkedIntArrays:Int()
  444.         Local chunk := CHUNK_SIZE
  445.         Local array1:Int[0]
  446.         Local iterations := INT_ARRAY_ITERATIONS
  447.         Local count := 0
  448.        
  449.         array1 = new Int[CHUNK_SIZE]
  450.         For Local index := 0 Until iterations
  451.             if count = array1.Length
  452.                 array1 = array1.Resize(array1.Length + CHUNK_SIZE)
  453.             Endif
  454.             array1[index] = count
  455.             count += 1
  456.         Next
  457.        
  458.         'final trim
  459.         If count < array1.Length
  460.             array1 = array1.Resize(count)
  461.         Endif
  462.        
  463.         return count
  464.     End
  465.    
  466.     Method TestChunkedFloatArrays:Int()
  467.         Local chunk := CHUNK_SIZE
  468.         Local array1:Float[0]
  469.         Local iterations := FLOAT_ARRAY_ITERATIONS
  470.         Local count := 0
  471.        
  472.         array1 = new Float[CHUNK_SIZE]
  473.         For Local index := 0 Until iterations
  474.             if count = array1.Length
  475.                 array1 = array1.Resize(array1.Length + CHUNK_SIZE)
  476.             Endif
  477.             array1[index] = Float(count)+0.345
  478.             count += 1
  479.         Next
  480.        
  481.         'final trim
  482.         If count < array1.Length
  483.             array1 = array1.Resize(count)
  484.         Endif
  485.        
  486.         return count
  487.     End
  488.    
  489.     Method TestChunkedStringArrays:Int()
  490.         Local chunk := CHUNK_SIZE
  491.         Local array1:String[0]
  492.         Local iterations := STRING_ARRAY_ITERATIONS
  493.         Local count := 0
  494.        
  495.         array1 = new String[CHUNK_SIZE]
  496.         For Local index := 0 Until iterations
  497.             if count = array1.Length
  498.                 array1 = array1.Resize(array1.Length + CHUNK_SIZE)
  499.             Endif
  500.             array1[index] = "value"+count
  501.             count += 1
  502.         Next
  503.        
  504.         'final trim
  505.         If count < array1.Length
  506.             array1 = array1.Resize(count)
  507.         Endif
  508.        
  509.         return count
  510.     End
  511. End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement