How to BEST create test data ...

Hubert Brandel

Active Member
Licensed User
Longtime User
Hi,

i want to build a Test-App to test self build functions. In the Windows world I use Xbase++, and Testdata in an 2 dimension Array. Xbase++ can hold different datatypes in one Array, so this is fast and easy:

B4X:
// Xbase++
aTestData := { { "String 1", 5, 31.12.2001, .t. } , ;
               { "String 2", 10, NIL, .f. } }
// One Array with 2 Datarecords, each Type ( String, Number, Date, Logical )
for x := 1 to len(aTestData) // 1 to 2 Datarecords
    if aTestData[4] = MyFunc( aTestData[1],aTestData[2],aTestData[3] ) // Function Type locical
      ? "Test OK"
    else
        nError++
    endif
next
if nError > 0 
   ? "Errors",nError
endif

I know that B4A (like other Basics etc.) can NOT mix up data types in one Array, or ?
So I thougt about using a TYPE and a Array of this TYPE for each Datarecord.
B4X:
TYPE TestData( sString as String, nVal as Int, sDate as String, Expected as logical 
DIM aTestData(2) as TestData ' I don't like starting with 0 ...
...
But how to geht the test data inside without typing so much source code ...

One other thing, the String parameters are often "" (String with lenght 0), this will generate runtime errors if I use this Strings. Isn't an empty string a normal string ?

I use normaly this to detect, but I got strange results:

B4X:
if sString.length = 0 ...
if sString = "" ...

Whats the best way to test if an string is empty without getting runtime errors.


Any hints ?
 

Hubert Brandel

Active Member
Licensed User
Longtime User
Thanks,

I did it this way, fast to type in the testdata and testing.


B4X:
Sub Globals
   Type TestDaten ( nWert As Int , sGeNr As String, sJahr As String, sName As String, sOrt As String, nShouldBe As Int )
   Dim aTDG As List 
   aTDG.Initialize

End Sub

Sub AddTestDaten(  nWert As Int , sGeNr As String, sJahr As String, sName As String, sOrt As String, nShouldBe As Int )
   Dim aTD As TestDaten
   aTD.Initialize
   
   aTD.nWert = nWert
   aTD.sGeNr = sGeNr
   aTD.sJahr  = sJahr
   aTD.sName = sName
   aTD.sOrt =  sOrt 
   aTD.nShouldBe = nShouldBe
   
   aTDG.Add(aTD)   

End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim nError, nErgebnis, nFehler, i As Int
   Dim aTD As TestDaten
   aTD.Initialize
   
   Msgbox("Testdaten generieren","Info")
   
   aTDG.Clear
   AddTestDaten(123456, "123456", 2012, "PRD34", "5182", 0) ' Return 0 is OK
   AddTestDaten(123456, "123456", 2012, "RDWKQ", "5183", 7) ' Error 7
   AddTestDaten(123456, "123456", 2012, "WQR123", "6183", 8) ' Error 8
... all needed value combinations
   Msgbox("start " & aTDG.Size & " tests","Info")
   
   For i = 0 To aTDG.Size-1
      aTD = aTDG.Get(i)
      Log( "Test " & i & ": " & aTD )   
      nErgebnis = TestType.CheckData ( aTD.nWert, aTD.sGeNr, aTD.sJahr, aTD.sName, aTD.sOrt)
      Log( " => " & nErgebnis )   
      If nErgebnis = aTD.nErwartet Then 
         Msgbox( aTD, "Test " & i+1 & " OK !")
      Else
         nFehler = nFehler + 1 
         Msgbox(nErgebnis & " # " & aTD.nShouldBe & Chr(13) & aTD.nWert & Chr(13) & aTD.sGeNr & Chr(13) & aTD.sJahr  _
                                                      & Chr(13) & aTD.sName & Chr(13) & aTD.sOrt, "Error " & nFehler )
      End If 
   Next
   
   If nError = 0 Then   
      Msgbox("SUPER, all tests OK !","Info")
   Else
      Msgbox("Fehler: " & nFehler & " !","Info")
   End If
   
End Sub

PS: I had to change some variable names in this example, if I forgot one, just igonre it ;-)
 
Upvote 0

Hubert Brandel

Active Member
Licensed User
Longtime User
Is there a shorter way to increase or decrease a variable ?
B4X:
dim nFehler as int
...
nFehler = nFehler + 1

I knew this syntax from other compilers:
B4X:
nFehler++ ' as nFehler = nFehler + 1
nFehler += 5 ' as nFehler = nFehler + 5

OR 

INCR nFehler
INCR nFehler, 5

AND
nFehler--
nFehler -= 5

DECR nFehler
DECR nFehler,5
 
Upvote 0
Top