!----------------------------------------------------------------------
!     Copyright (c) 2019 Shin-ichi Takehiro. All rights reserved.
!----------------------------------------------------------------------
!
!表題  w_base_mpi_module_sypack テストプログラム :: 終了処理のテスト
!
!履歴  2019/03/19  竹広真一
!
program w_mpi_module_finalize_test

  use dc_message, only : MessageNotify
  use dc_test, only : AssertEqual
  use w_mpi_module
  use mpi
  implicit none

  integer :: im(2)=(/128,64/)
  integer :: jm(2)=(/64,32/)
  integer :: nm(2)=(/42,21/)
!!$  integer :: im(2)=(/64,128/)
!!$  integer :: jm(2)=(/32,64/)
!!$  integer :: nm(2)=(/21,42/)

  real(8), allocatable  :: w_data(:)
  real(8), allocatable  :: xv_data(:,:)
  real(8), allocatable  :: xv_Ddata(:,:)

  ! 判定誤差設定
  integer, parameter :: check_digits = 11
  integer, parameter :: ignore = -12

  real(8) :: pi
  integer :: iproc, np, ierr

  integer :: n

  pi = atan(1.0D0)*4.0D0

  call MessageNotify('M','w_base_mpi_module_sypack_finalize_test', &
                         'w_base_mpi_module_sypack finalize subroutine tests')

 !---------------- MPI スタート ---------------------
  call MPI_INIT(IERR)
  call MPI_COMM_RANK(MPI_COMM_WORLD,IPROC,IERR)
  call MPI_COMM_SIZE(MPI_COMM_WORLD,NP,IERR)

  do n=1,2

  call w_mpi_Initial( nm(n), im(n), jm(n) )

  write(6,*) jc,nc,v_Lat
  write(6,*) l_nm(1,0)

  allocate(w_data(nc))
  allocate(xv_data(0:im(n)-1,jc))
  allocate(xv_Ddata(0:im(n)-1,jc))

  !========================= xv_w, w_xv のテスト===========================

  call MessageNotify('M','w_base_mpi_module_sypack_test', &
                         'w_base_mpi_module_sypack functions xv_w, w_xv tests')
  !==================== Y_1^0 のテスト ====================
  xv_data = sqrt(3.0D0)*sin(xv_Lat)                      ! Y_1^0
  w_data= 0.0D0 ; if ( l_nm(1,0) /= 0 ) w_data(l_nm(1,0))=1.0D0

  !---- 通常変換 ----
  call AssertEqual(&
    message='Y_1^0 Test of w_xv(xv_data)',                        &
    answer = w_data,                                              &
    check = w_xv(xv_data),                                        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data))',                  &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = 0.0D0
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data,iflag=-1))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data),iflag=-1)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = sqrt(3.0D0) * ( cos(xv_Lat)**2 - 2*sin(xv_Lat)**2 )
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data,iflag=1,ipow=0))',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = sqrt(3.0D0)*cos(xv_Lat)
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = sqrt(3.0D0) * sin(xv_Lat)**2
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data,iflag=2))',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^0 Test of xv_w(w_xv(xv_data),iflag=2)',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','Test of Y_1^0','Test of Y_1^0 succeeded!')

  !==================== Y_1^1 のテスト ====================
  xv_data = sqrt(3.0D0/2)*cos(xv_Lat)*cos(xv_Lon)          ! Y_1^1
  w_data= 0.0D0 ; if ( l_nm(1,1) /= 0 ) w_data(l_nm(1,1))=1.0D0/2.0D0

  !---- 通常変換 ----
  call AssertEqual(&
    message='Y_1^1 Test of w_xv(xv_data)',                        &
    answer = w_data,                                              &
    check = w_xv(xv_data),                                        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data))',                  &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = -sqrt(3.0D0/2)*cos(xv_Lat)*sin(xv_Lon)          ! Y_1^1
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data,iflag=-1))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data),iflag=-1)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = - sqrt(3.0D0/2) * 3 * sin(xv_Lat) * cos(xv_Lat) * cos(xv_Lon)
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data,iflag=1,ipow=0))',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = - sqrt(3.0D0/2) * sin(xv_Lat) * cos(xv_Lon)
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = sqrt(3.0D0/2) * sin(xv_Lat)* cos(xv_Lat) * cos(xv_Lon)
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data,iflag=2))',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data),iflag=2)',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','Test of Y_1^1','Test of Y_1^1 succeeded!')

  !==================== Y_1^{-1} のテスト ====================
  xv_data = -sqrt(3.0D0/2)*cos(xv_Lat)*sin(xv_Lon)         ! Y_1^{-1}
  w_data= 0.0D0 ; if ( l_nm(1,-1) /= 0 ) w_data(l_nm(1,-1))=1.0D0/2.0D0

  !---- 通常変換 ----
  call AssertEqual(&
    message='Y_1^1 Test of w_xv(xv_data)',                        &
    answer = w_data,                                              &
    check = w_xv(xv_data),                                        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data))',                  &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = -sqrt(3.0D0/2)*cos(xv_Lat)*cos(xv_Lon)          ! Y_1^{-1}
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data,iflag=-1))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data),iflag=-1)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = sqrt(3.0D0/2) * 3 * sin(xv_Lat) * cos(xv_Lat) * sin(xv_Lon)
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data,iflag=1,ipow=0))',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = sqrt(3.0D0/2) * sin(xv_Lat) * sin(xv_Lon)
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = - sqrt(3.0D0/2) * sin(xv_Lat)* cos(xv_Lat) * sin(xv_Lon)
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data,iflag=2))',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_1^1 Test of xv_w(w_xv(xv_data),iflag=2)',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !==================== Y_2^0 のテスト ====================
  xv_data = sqrt(5.0D0)*(3.0/2*sin(xv_Lat)**2-1/2.0)                 ! Y_2^0
  w_data= 0.0D0 ; if ( l_nm(2,0) /= 0 ) w_data(l_nm(2,0))=1.0D0

  !---- 通常変換 ----
  call AssertEqual(&
    message='Y_2^0 Test of w_xv(xv_data)',                        &
    answer = w_data,                                              &
    check = w_xv(xv_data),                                        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data))',                  &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = 0.0D0
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data,iflag=-1))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data),iflag=-1)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = sqrt(5.0D0)*(   3.0*sin(xv_Lat)*cos(xv_Lat)**2  &
                           - 3.0*sin(xv_Lat)**3 + sin(xv_Lat)  )
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data,iflag=1,ipow=0))',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = sqrt(5.0D0)*3.0*sin(xv_Lat)*cos(xv_Lat)
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = sqrt(5.0D0)*(3.0/2*sin(xv_Lat)**2-1/2.0)*sin(xv_Lat)
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data,iflag=2))',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^0 Test of xv_w(w_xv(xv_data),iflag=2)',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','Test of Y_2^0','Test of Y_2^0 succeeded!')

  !==================== Y_2^1 のテスト ====================
  xv_data = sqrt(5.0D0/6)*3.0*sin(xv_Lat)*cos(xv_Lat) * cos(xv_Lon)! Y_2^1
  w_data= 0.0D0 ; if ( l_nm(2,1) /= 0 ) w_data(l_nm(2,1))=1.0D0/2.0D0

  !---- 通常変換 ----
  call AssertEqual(&
    message='Y_2^1 Test of w_xv(xv_data)',                        &
    answer = w_data,                                              &
    check = w_xv(xv_data),                                        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^1 Test of xv_w(w_xv(xv_data))',                  &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = -sqrt(5.0D0/6)*3.0*sin(xv_Lat)*cos(xv_Lat)*sin(xv_Lon)! Y_2^1
  call AssertEqual(&
    message='Y_2^1 Test of xv_w(w_xv(xv_data,iflag=-1))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^1 Test of xv_w(w_xv(xv_data),iflag=-1)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = sqrt(5.0D0/6)*3.0 &
       *(cos(xv_Lat)**3-3*sin(xv_Lat)**2*cos(xv_Lat))*cos(xv_Lon)
  call AssertEqual(&
    message='Y_2^1 Test of xv_w(w_xv(xv_data,iflag=1,ipow=1))',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = sqrt(5.0D0/6)*3.0 * cos(2*xv_Lat) * cos(xv_Lon)
  call AssertEqual(&
    message='Y_2^1 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = sqrt(5.0D0/6)*3.0*sin(xv_Lat)**2*cos(xv_Lat) * cos(xv_Lon)
  call AssertEqual(&
    message='Y_2^1 Test of xv_w(w_xv(xv_data,iflag=2))',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^1 Test of xv_w(w_xv(xv_data),iflag=2)',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','Test of Y_2^1','Test of Y_2^1 succeeded!')

  !==================== Y_2^-1 のテスト ====================
  xv_data = -sqrt(5.0D0/6)*3.0*sin(xv_Lat)*cos(xv_Lat) * sin(xv_Lon)! Y_2^-1
  w_data= 0.0D0 ; if ( l_nm(2,-1) /= 0 ) w_data(l_nm(2,-1))=1.0D0/2.0D0

  !---- 通常変換 ----
  call AssertEqual(&
    message='Y_2^-1 Test of w_xv(xv_data)',                       &
    answer = w_data,                                              &
    check = w_xv(xv_data),                                        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^-1 Test of xv_w(w_xv(xv_data))',                 &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = -sqrt(5.0D0/6)*3.0*sin(xv_Lat)*cos(xv_Lat)*cos(xv_Lon)! Y_2^-1
  call AssertEqual(&
    message='Y_2^-1 Test of xv_w(w_xv(xv_data,iflag=-1))',        &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^-1 Test of xv_w(w_xv(xv_data),iflag=-1)',        &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = -sqrt(5.0D0/6)*3.0 &
       *(cos(xv_Lat)**3-3*sin(xv_Lat)**2*cos(xv_Lat))*sin(xv_Lon)
  call AssertEqual(&
    message='Y_2^-1 Test of xv_w(w_xv(xv_data,iflag=1,ipow=0))',  &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = -sqrt(5.0D0/6)*3.0 * cos(2*xv_Lat) * sin(xv_Lon)
  call AssertEqual(&
    message='Y_2^-1 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',  &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = -sqrt(5.0D0/6)*3.0*sin(xv_Lat)**2*cos(xv_Lat) * sin(xv_Lon)
  call AssertEqual(&
    message='Y_2^-1 Test of xv_w(w_xv(xv_data,iflag=2))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^-1 Test of xv_w(w_xv(xv_data),iflag=2)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','Test of Y_2^-1','Test of Y_2^-1 succeeded!')

  !==================== Y_2^2 のテスト ====================
  xv_data = sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2*cos(2*xv_Lon)         ! Y_2^2
  w_data= 0.0D0 ; if ( l_nm(2,2) /= 0 ) w_data(l_nm(2,2))=1.0D0/2.0D0

  !---- 通常変換 ----
  call AssertEqual(&
    message='Y_2^2 Test of w_xv(xv_data)',                        &
    answer = w_data,                                              &
    check = w_xv(xv_data),                                        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data))',                  &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2* (-2)* sin(2*xv_Lon)  ! Y_2^2
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data,iflag=-1))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data),iflag=-1)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = - sqrt(5.0D0/24)*3.0*4*sin(xv_Lat)*cos(xv_Lat)**2*cos(2*xv_Lon)
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data,iflag=1,ipow=0))',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = -sqrt(5.0D0/24)*3.0*2*cos(xv_Lat)*sin(xv_Lat)*cos(2*xv_Lon)
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',   &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2*sin(xv_Lat)*cos(2*xv_Lon)
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data,iflag=2))',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^2 Test of xv_w(w_xv(xv_data),iflag=2)',          &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','Test of Y_2^2','Test of Y_2^2 succeeded!')

  !==================== Y_2^-2 のテスト ====================
  xv_data = -sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2*sin(2*xv_Lon)         ! Y_2^-2
  w_data= 0.0D0 ; if ( l_nm(2,-2) /= 0 ) w_data(l_nm(2,-2))=1.0D0/2.0D0

  !---- 通常変換 ----
  call AssertEqual(&
    message='Y_2^-2 Test of w_xv(xv_data)',                       &
    answer = w_data,                                              &
    check = w_xv(xv_data),                                        &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data))',                 &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- X 微分変換 ----
  xv_Ddata = -sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2* 2*cos(2*xv_Lon)  ! Y_2^2
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data,iflag=-1))',        &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=-1)),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data),iflag=-1)',        &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=-1),                         &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_Ddata = sqrt(5.0D0/24)*3.0*4*sin(xv_Lat)*cos(xv_Lat)**2*sin(2*xv_Lon)
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data,iflag=1,ipow=0))',  &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=0)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_Ddata = sqrt(5.0D0/24)*3.0*2*cos(xv_Lat)*sin(xv_Lat)*sin(2*xv_Lon)
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data),iflag=1,ipow=1)',  &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=1),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- SIN 掛け算変換 ----
  xv_Ddata = -sqrt(5.0D0/24)*3.0*cos(xv_Lat)**2*sin(xv_Lat)*sin(2*xv_Lon)
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data,iflag=2))',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=2)),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )
  call AssertEqual(&
    message='Y_2^-2 Test of xv_w(w_xv(xv_data),iflag=2)',         &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=2),                          &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  call MessageNotify('M','Test of Y_2^-2','Test of Y_2^-2 succeeded!')

  !==================== 一般的関数のテスト ====================
  xv_data = cos(2*xv_Lon-pi/3) &
       *(sin(xv_Lat)-1)**2*(sin(xv_Lat)-0.5)*(sin(xv_Lat)+1)

  !---- 通常変換 ----
  call AssertEqual(&
    message='general function of xv_w(w_xv(xv_data))',            &
    answer = xv_data,                                             &
    check = xv_w(w_xv(xv_data)),                                  &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  !---- Y 微分変換 ----
  xv_data  = cos(xv_Lat)
  xv_Ddata = -2*sin(xv_Lat)

  call AssertEqual(&
    message='general function test of xv_w(w_xv(xv_data,iflag=1,ipow=1))',  &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=1)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_data  = cos(xv_Lat)**2
  xv_Ddata = -2*sin(xv_Lat)
  call AssertEqual(&
    message='general function test of xv_w(w_xv(xv_data,iflag=1,ipow=2))',  &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data,iflag=1,ipow=2)),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_data  = sin(xv_Lat)
  xv_Ddata = cos(xv_Lat)**2

  call AssertEqual(&
    message='general function test of xv_w(w_xv(xv_data),iflag=1,ipow=0)',  &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=0),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  xv_data  = cos(xv_Lat)**2
  xv_Ddata = -2.0D0 * sin(xv_Lat)

  call AssertEqual(&
    message='general function test of xv_w(w_xv(xv_data),iflag=1,ipow=2)',  &
    answer = xv_Ddata,                                            &
    check = xv_w(w_xv(xv_data),iflag=1,ipow=2),                   &
    significant_digits = check_digits, ignore_digits = ignore     &
    )

  CALL MPI_Barrier(MPI_COMM_WORLD, IERR)

  deallocate(w_data)
  deallocate(xv_data)
  deallocate(xv_Ddata)

  call w_mpi_Finalize

  CALL MPI_Barrier(MPI_COMM_WORLD, IERR)

  end do
 !------ MPIの終了 ------

  call MPI_FINALIZE(IERR)

  call MessageNotify('M','w_base_mpi_module_sypack_finalize_test', &
       'w_base_mpi_module_sypack finalize subroutine tests succeeded.')

end program w_mpi_module_finalize_test
