My idea is to make some testing system for functions, as per the model of CodeForces or TopCoder, but in VBA. There, the users write functions for a problem and the problems are tested vs. predefined tests.
In my case, it will be exactly the same, but the tests would be written on Notepad files.
Long story short, you have to write the function and then the function would be feeded with input parameters from one textfile and the result would be compared with the result from another test file.
The idea of the functions, is that they would be written by a "competitor", as in the aforementioned sites. The functions would be the answer of a predefined problem.
So, let's say that we have defined a problem, which is solvable through this:
If c Mod 2 = 0 Then
MainTest = a + b + c
Else
MainTest = a + b - c
End If
Thus, we need to write 2 notepad files in advance, which consist of the input parameters for a, b and c and the results. These are the notepad files:
2 2 2
2 2
2 2 3
4 54 1
2 2
54 23 6
45 45 10
File with results:
6
1
1
58
100
121
100
To make it more presentable, the file with the results, has some runtime errors and wrong calculation errors. Thus this is the result in the immediate window:
Test 1............................................ ok!
Runtime error on 2!
Test 3............................................ ok!
Error on test 4! Expected -> 57 Received -> 58
Runtime error on 5!
Error on test 6! Expected -> 83 Received -> 121
Test 7............................................ ok!
Another possible predefined problem may sound like - "Give me the next character of a string.". Thus "a b c d" would result to "b c d e" and "a z" would be "b a".
And this is the whole competitive testing system:
Option Explicit
Public Sub Main()
Dim totalTests As Long
Dim pathInputTests As String
Dim pathOutputTests As String
Dim inputTests As Variant
Dim outputTests As Variant
Dim cntTests As Long
Dim cnt As Long
pathInputTests = "C:\Desktop\Test001.txt"
pathOutputTests = "C:\Desktop\Result001.txt"
inputTests = Split(ReadFileLineByLineToString(pathInputTests), vbCrLf)
outputTests = Split(ReadFileLineByLineToString(pathOutputTests), vbCrLf)
For cnt = LBound(inputTests) To UBound(inputTests)
Dim expectedValue As Variant
Dim receivedValue As Variant
On Error Resume Next
expectedValue = MainTest(inputTests(cnt))
receivedValue = outputTests(cnt)
If Err.Number <> 0 Then
Debug.Print runtimeError(cnt)
Err.Clear
Else
If expectedValue = receivedValue Then
Debug.Print positiveResult(cnt)
Else
Debug.Print negativeResult(cnt, expectedValue, receivedValue)
End If
End If
Next cnt
End Sub
Public Function runtimeError(ByVal cnt As Long) As String
cnt = cnt + 1
runtimeError = "Runtime error on " & cnt & "!"
End Function
Public Function positiveResult(ByVal cnt As Long) As String
cnt = cnt + 1
positiveResult = "Test " & cnt & "..................................... ok!"
End Function
Public Function negativeResult(ByVal cnt As Long, expected As Variant, _
received As Variant) As String
cnt = cnt + 1
negativeResult = "Error on test " & cnt & "!" & _
" Expected -> " & vbTab & expected & vbTab & _
" Received -> " & vbTab & received
End Function
'---------------------------------------------------------------------------------------
' Method : MainTest
' Purpose: This is where the competitors paste their solution.
'---------------------------------------------------------------------------------------
Public Function MainTest(ByVal consoleInput As String) As String
Dim a As Double
Dim b As Double
Dim c As Double
a = Split(consoleInput)(0)
b = Split(consoleInput)(1)
c = Split(consoleInput)(2)
If c Mod 2 = 0 Then
MainTest = a + b + c
Else
MainTest = a + b - c
End If
End Function
Public Function ReadFileLineByLineToString(path As String) As String
Dim fileNo As Long
fileNo = FreeFile
Open path For Input As #fileNo
Do While Not EOF(fileNo)
Dim textRowInput As String
Line Input #fileNo, textRowInput
ReadFileLineByLineToString = ReadFileLineByLineToString & textRowInput
If Not EOF(fileNo) Then
ReadFileLineByLineToString = ReadFileLineByLineToString & vbCrLf
End If
Loop
Close #fileNo
End Function
The code with the 4 test files is in GitHub here - https://github.com/Vitosh/VBA_personal/tree/master/AlgorithmsWithVBA - feel free to make push requests if you feel like it! :)