Unit Tests

Unit Testing natively

TODO:

TODO

  • Unit test examples
  • TAP
  • Proove to run lots
  • Output from Proove with lots of tests

Example

! Local unit test

subroutine unit_main ()

! TEMPLATE Create any integers etc.
character(len=256) :: param1
character(len=256) :: charvar
integer diag_int
integer*4 diag_int4
double precision diag_double
INTEGER testint(32)

! TEMPLATE Plan some tests
call unit_plan(38)

call unit_starttime()

! TEMPALATE Example of how you can read a command line param
call unit_param(1, param1)
call unit_diag_string("param 1", param1)
! print *, param1

! Check too long - aka f90 vs ftn
call unit_diag_string("String Testing 123", "Here it is, a very long string that should stilll work because f90")
call unit_diag_string("String Testing + split",
+ "Here it is, a very long string that should stilll work because f90")
! TODO - should be supported, look at f90 config
!call unit_diag_string("String Testing & split" &
!      ,"Here it is, a very long string that should stilll work because f90")

! Testing integer strings
call mvwrd('test', testint, 2)
call unit_diag(testint)
call unit_diag_string("TestInt", testint)

! TEMPLATE Write your tests
call unit_diag("These tests should pass")
call unit_diag_string("String", "Here it is")
diag_int = 42
call unit_diag_integer("Integer", diag_int)
diag_int4 = 987654321
call unit_diag_integer4("Integer4", diag_int4)
! XXX Not working
! call unit_cmp_ok(diag_int, ">", 40, "Greater than 40")
diag_double = 54.321
call unit_diag_double("Double", diag_double)
! XXX Not working
! call unit_cmp_ok(diag_double, ">", 50, "Greater than 50")

! (leave this out) call unit_ok(1==0, "Should Fail")
call unit_ok(1, "First test")
call unit_is("fred", "fred", "Fred test was good")
call unit_cmp_ok(3, ">", 1, "Cmpare using >, <. ==, >= etc")
!call unit_cmp_ok(irc, "==", 7, "Cmpare using >, <. ==, >= etc")
!call unit_is_integer(irc, 7, "not found")

call unit_like("strange", "range", "strange ~~ /range/")
call unit_unlike("strange", "anger", "strange !~~ /anger/")
call unit_like("stranger", "^s.(r).*$", "matches the regex")

charvar = "ABC123"
call unit_diag_string("Variable", charvar)
call unit_ok(1, charvar)

call unit_ok(1, "OK 1")
integer testn
testn = 2
call unit_cmp_ok(testn, "==", 2, "OK int==2")

! Directory
call unit_dir_exists("/tmp", "Temp exists string")
charvar = "/tmp"
call unit_dir_exists(charvar, "Temp exists variable")
call unit_dir_notexists("/notexists", "Not exists string")

! File
call unit_file_exists("file.txt", "File exists")
call unit_file_notexists("/never.txt", "Not exists file")
call unit_file_notexists("/tmp", "Not exists file but is dir")

! File like
call unit_file_like("./file.txt", "z", 0, "File has no z")
call unit_file_like("./file.txt", "o", 3, "File has three o")
call unit_file_like("./file.txt", "th", 6, "File has five th")
call unit_file_like("./file.txt", ".i.th$", 2, "File has two .i.th")
call unit_file_like("./file.txt", ".i.th", 2, "File has two .i.th")
call unit_file_like("./file.txt", "Eight", 2, "two eight")
call unit_file_like("./file.txt", "^Eight", 1, "one eight start")
call unit_file_like("./file.txt", "^This.......$", 1, "one eight start")
call unit_file_like("./file.txt", "^This.{7}$", 1, "one eight start adv")
call unit_file_like("./file.txt", "^This.{6}$", 0, "one eight start adv")
call unit_file_like("./file.txt", "\d", 2, "2 digits")
call unit_file_like("./file.txt", "1[[:digit:]]{3}", 1, "1 digits")
call unit_file_like("./file.txt", "^.{82}$", 1, "1 82 char record")

! Check Sizes - size of character, array int and single int
call unit_size(charvar, 256, "Charvar is 256")
call unit_size(testint, 64, "Testint is 64")
call unit_size(diag_int, 2, "Single int is 2")

call unit_stoptime("Time?")

! Test double 
double precision expected_in, test_in
expected_in  = 0.6d0
test_in = 0.0d0
call unit_cmp_ok_double(test_in, "!=", expected_in, "Should not match")
expected_in  = 0.6d0
test_in  = 0.6000331d0
call unit_cmp_ok_double(test_in, "==", expected_in, "Should match")
call unit_cmp_ok_double(test_in, ">=", expected_in, "GT =")
call unit_cmp_ok_double(test_in, "<=", expected_in, "LT =")
expected_in  = 0.6d0
test_in  = 0.7d0
call unit_cmp_ok_double(test_in, ">", expected_in, "Should GT")
expected_in  = 0.8d0
test_in  = 0.7d0
call unit_cmp_ok_double(test_in, "<", expected_in, "Should LT")

if (unit_nodiag()) then
  print "(A)", "# NO DIAG ENABLED"
end if

if (.not. unit_nodiag()) then
  print "(A)", "# DIAG STILL ENABLED"
end if


! XXX Errors that hard to catch - C params
!call err$p('hello world~',,0,0,-1)

! Large like
call unit_like(
+ "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij"
+ "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij"
+ "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij"
+ "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij"
+ "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij"
+ "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij"
+ , "abc", "long string worked")

! TEMPLATE End
call unit_done_testing()

return

end subroutine unit_main