!-------------------------------------------------------------------------
! Copyright (c) 2017 SPMODEL Development Group. All rights reserved.!
!-------------------------------------------------------------------------
!
!表題  asc_module テストプログラム
!
!履歴  2017/05/18  竹広真一
!
program asc_test_base1d

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use asc_module
  implicit none
  integer, parameter :: im=32, km=14
  real(8), parameter :: xmin=0.0D0, xmax=1.0D0
  real(8), parameter :: pi=3.1415926535897932385D0
  real(8), dimension(0:im) :: g_f, g_ans
  real(8), dimension(km)   :: s_f_sol
  real(8), dimension(0:km) :: c_f_sol
  ! 判定誤差設定
  integer, parameter :: check_digits = 10
  integer, parameter :: ignore = -11


  call MessageNotify('M','asc_test_base1d','asc_module 1-dim function tests')

  call asc_initial(im,km,xmin,xmax)

  !----------- test 1 ---------------

  call MessageNotify('M','asc_test_base1d', &
       'Test 1: g_f = 1.0D0')

  g_f = 1.0D0;  g_ans=0.0D0
  c_f_sol = 0.0D0 ; c_f_sol(0)=2.0D0

  call AssertEqual(&
    message='Test of c_g(g_f)',                                   &
    answer = c_f_sol,                                             &
    check = c_g(g_f),                                             &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='Test of g_c(c_g(g_f)',                               &
    answer = g_f,                                                 &
    check  = g_c(c_g(g_f)),                                       &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call AssertEqual(&
    message='Test of g_s(s_Dx_c(c_g(g_f)))',                      &
    answer = g_ans,                                               &
    check  = g_s(s_Dx_c(c_g(g_f))),                               &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !----------- test 2 ---------------


  call MessageNotify('M','asc_test_base1d', &
       'Test 2: g_f = sin(\pi*x)')

  g_f = sin(pi*g_x);  g_ans = pi*cos(pi*g_x)
  s_f_sol = 0.0D0 ; s_f_sol(1)=1.0D0

  call AssertEqual(&
    message='Test of s_g(g_f)',                                 &
    answer = s_f_sol,                                           &
    check = s_g(g_f),                                                &
    significant_digits = check_digits, ignore_digits = ignore   &
    )

  call AssertEqual(&
    message='Test of g_s(s_g(g_f))',                            &
    answer = g_f,                                               &
    check = g_s(s_g(g_f)),                                           &
    significant_digits = check_digits, ignore_digits = ignore   &
    )

  call AssertEqual(&
    message='Test of g_c(c_dx_s(s_g(g_f)))',                    &
    answer = g_ans,                                             &
    check = g_c(c_Dx_s(s_g(g_f))),                              &
    significant_digits = check_digits, ignore_digits = ignore   &
    )


  !----------- test 3 ---------------

  call MessageNotify('M','asc_test_base1d', &
    'Test 3: g_f = cos(4*\pi*x)')

  g_f = cos(4.0D0*pi*g_x); g_ans = - (4.0D0*pi)**2*g_f
  c_f_sol = 0.0D0 ; c_f_sol(4)=1.0D0

  call AssertEqual(&
    message='Test of c_g(g_f)',                                 &
    answer = c_f_sol,                                           &
    check = c_g(g_f),                                                &
    significant_digits = check_digits, ignore_digits = ignore   &
    )

  call AssertEqual(&
    message='Test of g_c(c_g(g_f))',                            &
    answer = g_f,                                               &
    check = g_c(c_g(g_f)),                                           &
    significant_digits = check_digits, ignore_digits = ignore   &
    )

  call AssertEqual(&
    message='Test of g_e(e_dx_e(e_dx_e(e_f))) ',                &
    answer = g_ans,                                             &
    check = g_c(c_Dx_s(s_Dx_c(c_g(g_f)))),                      &
    significant_digits = check_digits, ignore_digits = ignore   &
    )

  call MessageNotify('M','asc_test_base1d', &
       'asc_module 1-dim function tests succeeded!')

end program asc_test_base1d


