program test_bcd
  use BCD_TO_ASCII_M
  implicit none

  integer :: i, result, passed, failed
  character(63) :: test_encoding

  passed = 0
  failed = 0

  print *, "========================================="
  print *, "BCD_TO_ASCII_M Comprehensive Test Suite"
  print *, "========================================="
  print *, ""

  ! Test 1: Init_BCD_to_Ascii
  print *, "Test 1: Init_BCD_to_Ascii"
  print *, "-----------------------------------------"
  call Init_BCD_to_Ascii
  print *, "At character:   '", At, "'"
  print *, "Dev character:  '", Dev, "'"
  print *, "Hash character: '", Hash, "'"
  print *, "Plus character: '", Plus, "'"
  if (At /= ' ' .and. Hash /= ' ') then
    print *, "PASSED: Characters initialized"
    passed = passed + 1
  else
    print *, "FAILED: Characters not properly initialized"
    failed = failed + 1
  end if
  print *, ""

  ! Test 2: Fill_Encoding with Pierce A encoding
  print *, "Test 2: Fill_Encoding (Pierce A)"
  print *, "-----------------------------------------"
  call Fill_Encoding(Encodings(Pierce_A_Encoding))
  call Init_BCD_to_Ascii
  print *, "Pierce A - At: '", At, "' (should be @)"
  print *, "Pierce A - Hash: '", Hash, "' (should be #)"
  if (At == '@' .and. Hash == '#') then
    print *, "PASSED: Pierce A encoding"
    passed = passed + 1
  else
    print *, "FAILED: Pierce A encoding"
    failed = failed + 1
  end if
  print *, ""

  ! Test 3: Fill_Encoding with Pierce H encoding
  print *, "Test 3: Fill_Encoding (Pierce H)"
  print *, "-----------------------------------------"
  call Fill_Encoding(Encodings(Pierce_H_Encoding))
  call Init_BCD_to_Ascii
  print *, "Pierce H - At: '", At, "' (should be ')"
  print *, "Pierce H - Hash: '", Hash, "' (should be =)"
  if (At == '''' .and. Hash == '=') then
    print *, "PASSED: Pierce H encoding"
    passed = passed + 1
  else
    print *, "FAILED: Pierce H encoding"
    failed = failed + 1
  end if
  print *, ""

  ! Test 4: Fill_Encoding with SIMH encoding
  print *, "Test 4: Fill_Encoding (SIMH)"
  print *, "-----------------------------------------"
  call Fill_Encoding(Encodings(SIMH_Encoding))
  call Init_BCD_to_Ascii
  print *, "SIMH - At: '", At, "' (should be @)"
  print *, "SIMH - Hash: '", Hash, "' (should be #)"
  if (At == '@' .and. Hash == '#') then
    print *, "PASSED: SIMH encoding"
    passed = passed + 1
  else
    print *, "FAILED: SIMH encoding"
    failed = failed + 1
  end if
  print *, ""

  ! Reset to Pierce A for remaining tests
  call Fill_Encoding(Encodings(Pierce_A_Encoding))
  call Init_BCD_to_Ascii

  ! Test 5: Char_To_Digit with valid digits
  print *, "Test 5: Char_To_Digit - Valid Digits"
  print *, "-----------------------------------------"
  result = Char_To_Digit('0')
  print *, "'0' -> ", result, " (expected 0)"
  if (result == 0) then
    passed = passed + 1
  else
    failed = failed + 1
  end if

  result = Char_To_Digit('5')
  print *, "'5' -> ", result, " (expected 5)"
  if (result == 5) then
    passed = passed + 1
  else
    failed = failed + 1
  end if

  result = Char_To_Digit('9')
  print *, "'9' -> ", result, " (expected 9)"
  if (result == 9) then
    passed = passed + 1
  else
    failed = failed + 1
  end if
  print *, ""

  ! Test 6: Char_To_Digit with positive signed digits
  print *, "Test 6: Char_To_Digit - Positive Signed"
  print *, "-----------------------------------------"
  result = Char_To_Digit('A')
1 format ( 1x, a, sp, i12, a )
  print 1, "'A' -> ", result, " (expected +1)"
  if (result == 1) then
    passed = passed + 1
  else
    failed = failed + 1
  end if

  result = Char_To_Digit('E')
  print 1, "'E' -> ", result, " (expected +5)"
  if (result == 5) then
    passed = passed + 1
  else
    failed = failed + 1
  end if

  result = Char_To_Digit('I')
  print 1, "'I' -> ", result, " (expected +9)"
  if (result == 9) then
    passed = passed + 1
  else
    failed = failed + 1
  end if
  print *, ""

  ! Test 7: Char_To_Digit with negative signed digits
  print *, "Test 7: Char_To_Digit - Negative Signed"
  print *, "-----------------------------------------"
  result = Char_To_Digit('J')
  print *, "'J' -> ", result, " (expected -1)"
  if (result == -1) then
    passed = passed + 1
  else
    failed = failed + 1
  end if

  result = Char_To_Digit('N')
  print *, "'N' -> ", result, " (expected -5)"
  if (result == -5) then
    passed = passed + 1
  else
    failed = failed + 1
  end if

  result = Char_To_Digit('R')
  print *, "'R' -> ", result, " (expected -9)"
  if (result == -9) then
    passed = passed + 1
  else
    failed = failed + 1
  end if
  print *, ""

  ! Test 8: Char_To_Digit with special characters
  print *, "Test 8: Char_To_Digit - Special Chars"
  print *, "-----------------------------------------"
  result = Char_To_Digit('|')
  print 1, "'|' -> ", result, " (expected +0)"
  if (result == 0) then
    passed = passed + 1
  else
    failed = failed + 1
  end if

  result = Char_To_Digit('?')
  print 1, "'?' -> ", result, " (expected +0)"
  if (result == 0) then
    passed = passed + 1
  else
    failed = failed + 1
  end if
  print *, ""

  ! Test 9: Char_To_Digit with invalid character
  print *, "Test 9: Char_To_Digit - Invalid Char"
  print *, "-----------------------------------------"
  result = Char_To_Digit('&')
  print *, "'&' -> ", result, " (expected -100000)"
  if (result == -100000) then
    print *, "PASSED: Invalid character handled"
    passed = passed + 1
  else
    print *, "FAILED: Invalid character not handled"
    failed = failed + 1
  end if
  print *, ""

  ! Test 10: BCD_TO_ASCII array
  print *, "Test 10: BCD_TO_ASCII Array"
  print *, "-----------------------------------------"
  print *, "BCD 0 (blank):    '", BCD_TO_ASCII(0), "'"
  print *, "BCD 1 (1):        '", BCD_TO_ASCII(1), "'"
  print *, "BCD 10 (0):       '", BCD_TO_ASCII(10), "'"
  print *, "BCD 49 (A):       '", BCD_TO_ASCII(49), "'"
  print *, "BCD 33 (J):       '", BCD_TO_ASCII(33), "'"
  if (BCD_TO_ASCII(0) == ' ' .and. BCD_TO_ASCII(1) == '1' .and. &
      BCD_TO_ASCII(49) == 'A' .and. BCD_TO_ASCII(33) == 'J') then
    print *, "PASSED: BCD_TO_ASCII array"
    passed = passed + 1
  else
    print *, "FAILED: BCD_TO_ASCII array"
    failed = failed + 1
  end if
  print *, ""

  ! Test 11: ASCII_TO_BCD array
  print *, "Test 11: ASCII_TO_BCD Array"
  print *, "-----------------------------------------"
  i = iachar('0')
  print *, "ASCII '0' ->", ASCII_TO_BCD(i), " (expected 12 octal = 10 dec)"
  i = iachar('A')
  print *, "ASCII 'A' ->", ASCII_TO_BCD(i), " (expected 61 octal = 49 dec)"
  i = iachar('a')
  print *, "ASCII 'a' ->", ASCII_TO_BCD(i), " (expected 61 octal = 49 dec, same as A)"
  i = iachar('0')
  if (ASCII_TO_BCD(i) == 10) then
    passed = passed + 1
  else
    failed = failed + 1
  end if
  i = iachar('A')
  if (ASCII_TO_BCD(i) == 49) then
    passed = passed + 1
  else
    failed = failed + 1
  end if
  i = iachar('a')
  if (ASCII_TO_BCD(i) == 49) then
    print *, "PASSED: ASCII_TO_BCD handles lowercase"
    passed = passed + 1
  else
    print *, "FAILED: ASCII_TO_BCD lowercase handling"
    failed = failed + 1
  end if
  print *, ""

  ! Test 12: Round-trip conversion
  print *, "Test 12: Round-trip ASCII<->BCD"
  print *, "-----------------------------------------"
  i = iachar('Z')
  result = ASCII_TO_BCD(i)
  print *, "'Z' -> BCD", result, "-> '", BCD_TO_ASCII(result), "'"
  if (BCD_TO_ASCII(result) == 'Z') then
    print *, "PASSED: Round-trip conversion"
    passed = passed + 1
  else
    print *, "FAILED: Round-trip conversion"
    failed = failed + 1
  end if
  print *, ""

  ! Test 13: Encoding constants
  print *, "Test 13: Encoding Constants"
  print *, "-----------------------------------------"
  print *, "Pierce_A_Encoding =", Pierce_A_Encoding
  print *, "Pierce_H_Encoding =", Pierce_H_Encoding
  print *, "SIMH_Encoding =", SIMH_Encoding
  print *, "Icelandic_Encoding =", Icelandic_Encoding
  if (Pierce_A_Encoding == 1 .and. Pierce_H_Encoding == 2 .and. &
      SIMH_Encoding == 3 .and. Icelandic_Encoding == 4) then
    print *, "PASSED: Encoding constants"
    passed = passed + 1
  else
    print *, "FAILED: Encoding constants"
    failed = failed + 1
  end if
  print *, ""

  ! Summary
  print *, "========================================="
  print *, "Test Summary"
  print *, "========================================="
  print *, "Tests Passed:", passed
  print *, "Tests Failed:", failed
  print *, "Total Tests: ", passed + failed
  if (failed == 0) then
    print *, ""
    print *, "ALL TESTS PASSED!"
  else
    print *, ""
    print *, "SOME TESTS FAILED!"
  end if
  print *, "========================================="

end program test_bcd
