diff --git a/fpm.toml b/fpm.toml index 30efdd5..677daa6 100644 --- a/fpm.toml +++ b/fpm.toml @@ -14,7 +14,7 @@ source-form = "free" auto-executables = true auto-tests = true auto-examples = true -module-naming = true +module-naming = false [install] library = true @@ -25,4 +25,12 @@ main = "example_in_f.f90" [[example]] name = "example_in_c" -main = "example_in_c.c" \ No newline at end of file +main = "example_in_c.c" + +[dev-dependencies] + +[[test]] +name = "tester" +main = "tester.f90" +[test.dependencies] +test-drive = {git="https://github.com/fortran-lang/test-drive", tag="v0.4.0"} diff --git a/pywrapper/pycodata/tests/test.py b/pywrapper/pycodata/tests/test.py index 671d855..1e7bf63 100644 --- a/pywrapper/pycodata/tests/test.py +++ b/pywrapper/pycodata/tests/test.py @@ -1,14 +1,147 @@ import unittest from ..constants import * -class TestLib(unittest.TestCase): - r"""Test pyiawps library.""" - def test_value(self): - value = ALPHA_PARTICLE_MASS - reference = 6.6446573357e-27 - self.assertEqual(value, reference) +class TestYear(unittest.TestCase): + r"""Test year.""" def test_year(self): value = YEAR - reference = 2018 - self.assertEqual(value, reference) + expected = 2018 + self.assertEqual(value, expected) + + +class TestConstants(unittest.TestCase): + r"""Test constant values.""" + def test_ALPHA_PARTICLE_ELECTRON_MASS_RATIO(self): + value = ALPHA_PARTICLE_ELECTRON_MASS_RATIO + expected = 7294.29954142 + self.assertEqual(value, expected) + + def test_ALPHA_PARTICLE_MASS(self): + value = ALPHA_PARTICLE_MASS + expected = 6.6446573357e-27 + self.assertEqual(value, expected) + + def test_ATOMIC_MASS_CONSTANT(self): + value = ATOMIC_MASS_CONSTANT + expected = 1.66053906660e-27 + self.assertEqual(value, expected) + + def test_AVOGADRO_CONSTANT(self): + value = AVOGADRO_CONSTANT + expected = 6.02214076e23 + self.assertEqual(value, expected) + + def test_BOLTZMANN_CONSTANT(self): + value = BOLTZMANN_CONSTANT + expected = 1.380649e-23 + self.assertEqual(value, expected) + + def test_ELECTRON_VOLT(self): + value = ELECTRON_VOLT + expected = 1.602176634e-19 + self.assertEqual(value, expected) + + def test_ELEMENTARY_CHARGE(self): + value = ELEMENTARY_CHARGE + expected = 1.602176634e-19 + self.assertEqual(value, expected) + + def test_FARADAY_CONSTANT(self): + value = FARADAY_CONSTANT + expected = 96485.33212 + self.assertEqual(value, expected) + + def test_MOLAR_MASS_CONSTANT(self): + value = MOLAR_MASS_CONSTANT + expected = 0.99999999965e-3 + self.assertEqual(value, expected) + + def test_MOLAR_VOLUME_NTP(self): + value = MOLAR_VOLUME_OF_IDEAL_GAS__273_15_K__101_325_KPA + expected = 22.41396954e-3 + self.assertEqual(value, expected) + + def test_PLANCK_CONSTANT(self): + value = PLANCK_CONSTANT + expected = 6.62607015e-34 + self.assertEqual(value, expected) + + def test_SPEED_OF_LIGHT(self): + value = SPEED_OF_LIGHT_IN_VACUUM + expected = 299792458.0 + self.assertEqual(value, expected) + + def test_STANDARD_ACCELERATION_OF_GRAVITY(self): + value = STANDARD_ACCELERATION_OF_GRAVITY + expected = 9.80665 + self.assertEqual(value, expected) + + +class TestUncertainties(unittest.TestCase): + r"""Test constant uncertainties.""" + def test_U_ALPHA_PARTICLE_ELECTRON_MASS_RATIO(self): + value = U_ALPHA_PARTICLE_ELECTRON_MASS_RATIO + expected = 0.00000024 + self.assertEqual(value, expected) + + def test_U_ALPHA_PARTICLE_MASS(self): + value = U_ALPHA_PARTICLE_MASS + expected = 0.0000000020e-27 + self.assertEqual(value, expected) + + def test_U_ATOMIC_MASS_CONSTANT(self): + value = U_ATOMIC_MASS_CONSTANT + expected = 0.00000000050e-27 + self.assertEqual(value, expected) + + def test_U_AVOGADRO_CONSTANT(self): + value = U_AVOGADRO_CONSTANT + expected = 0.0 + self.assertEqual(value, expected) + + def test_U_BOLTZMANN_CONSTANT(self): + value = U_BOLTZMANN_CONSTANT + expected = 0.0 + self.assertEqual(value, expected) + + def test_U_ELECTRON_VOLT(self): + value = U_ELECTRON_VOLT + expected = 0.0 + self.assertEqual(value, expected) + + def test_U_ELEMENTARY_CHARGE(self): + value = U_ELEMENTARY_CHARGE + expected = 0.0 + self.assertEqual(value, expected) + + def test_U_FARADAY_CONSTANT(self): + value = U_FARADAY_CONSTANT + expected = 0.0 + self.assertEqual(value, expected) + + def test_U_MOLAR_MASS_CONSTANT(self): + value = U_MOLAR_MASS_CONSTANT + expected = 0.00000000030e-3 + self.assertEqual(value, expected) + + def test_U_MOLAR_VOLUME_NTP(self): + value = U_MOLAR_VOLUME_OF_IDEAL_GAS__273_15_K__101_325_KPA + expected = 0.0 + self.assertEqual(value, expected) + + def test_U_PLANCK_CONSTANT(self): + value = U_PLANCK_CONSTANT + expected = 0.0 + self.assertEqual(value, expected) + + def test_U_SPEED_OF_LIGHT(self): + value = U_SPEED_OF_LIGHT_IN_VACUUM + expected = 0.0 + self.assertEqual(value, expected) + + def test_U_STANDARD_ACCELERATION_OF_GRAVITY(self): + value = U_STANDARD_ACCELERATION_OF_GRAVITY + expected = 0.0 + self.assertEqual(value, expected) + diff --git a/test/test.f90 b/test/test.f90 deleted file mode 100644 index 6baa49d..0000000 --- a/test/test.f90 +++ /dev/null @@ -1,22 +0,0 @@ -program main - use iso_fortran_env - use codata - implicit none - - print *, "c=", speed_of_light_in_vacuum - print *, "u(c)=", u_speed_of_light_in_vacuum - print *, codata_version_version - -contains - -pure elemental function roundn(x, n)result(r) - implicit none - real(real64), intent(in) :: x - integer(int32), intent(in) :: n - real(real64) :: r - - r = anint(x * 10.0d0**n, kind=kind(r)) / 10.0d0**n - -end function - -end program main \ No newline at end of file diff --git a/test/tester.f90 b/test/tester.f90 new file mode 100644 index 0000000..49f3f82 --- /dev/null +++ b/test/tester.f90 @@ -0,0 +1,28 @@ +program tester + use iso_fortran_env + use testdrive, only : run_testsuite, new_testsuite, testsuite_type + use testsuite_constants, only : collect_suite_constants + use testsuite_uncertainties, only : collect_suite_uncertainties + use testsuite_year, only : collect_suite_year + implicit none + type(testsuite_type), allocatable :: testsuites(:) + character(len=*), parameter :: fmt = '("#", *(1x, a))' + integer :: stat, is + + stat = 0 + + testsuites = [new_testsuite("Year", collect_suite_year),& + new_testsuite("Constants", collect_suite_constants),& + new_testsuite("Uncertainties", collect_suite_uncertainties)] + + do is = 1, size(testsuites) + write(error_unit, fmt) "Testing:", testsuites(is)%name + call run_testsuite(testsuites(is)%collect, error_unit, stat) + end do + + if (stat > 0) then + write(error_unit, '(i0, 1x, a)') stat, "test(s) failed!" + error stop + end if + +end program diff --git a/test/testsuite_constants.f90 b/test/testsuite_constants.f90 new file mode 100644 index 0000000..ebec034 --- /dev/null +++ b/test/testsuite_constants.f90 @@ -0,0 +1,150 @@ +module testsuite_constants + !! Test some constants values + use iso_fortran_env + use testdrive, only : new_unittest, unittest_type, error_type, check + use codata + implicit none + private + + public :: collect_suite_constants + +contains + +subroutine collect_suite_constants(testsuite) + !! Collection of tests + type(unittest_type), allocatable, intent(out) :: testsuite(:) + + testsuite = [& + new_unittest("ALPHA_PARTICLE_ELECTRON_MASS_RATIO", test_ALPHA_PARTICLE_ELECTRON_MASS_RATIO),& + new_unittest("ALPHA_PARTICLE_MASS", test_ALPHA_PARTICLE_MASS),& + new_unittest("ATOMIC_MASS_CONSTANT", test_ATOMIC_MASS_CONSTANT),& + new_unittest("AVOGADRO_CONSTANT", test_AVOGADRO_CONSTANT),& + new_unittest("BOLTZMANN_CONSTANT", test_BOLTZMANN_CONSTANT),& + new_unittest("ELECTRON_VOLT", test_ELECTRON_VOLT),& + new_unittest("ELEMENTARY_CHARGE", test_ELEMENTARY_CHARGE),& + new_unittest("FARADAY_CONSTANT", test_FARADAY_CONSTANT),& + new_unittest("MOLAR_MASS_CONSTANT", test_MOLAR_MASS_CONSTANT),& + new_unittest("MOLAR_VOLUME_OF_IDEAL_GAS__273_15K__101_325_KPA", test_MOLAR_VOLUME_NTP),& + new_unittest("PLANCK_CONSTANT", test_PLANCK_CONSTANT),& + new_unittest("SPEED_OF_LIGHT_IN_VACUUM", test_SPEED_OF_LIGHT),& + new_unittest("STANDARD_ACCELERATION_OF_GRAVITY", test_STANDARD_ACCELERATION_OF_GRAVITY)] +end subroutine + +subroutine test_ALPHA_PARTICLE_ELECTRON_MASS_RATIO(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 7294.29954142d0 + value = ALPHA_PARTICLE_ELECTRON_MASS_RATIO + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_ALPHA_PARTICLE_MASS(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 6.6446573357d-27 + value = ALPHA_PARTICLE_MASS + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_ATOMIC_MASS_CONSTANT(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 1.66053906660d-27 + value = ATOMIC_MASS_CONSTANT + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_AVOGADRO_CONSTANT(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 6.02214076d23 + value = AVOGADRO_CONSTANT + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_BOLTZMANN_CONSTANT(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 1.380649d-23 + value = BOLTZMANN_CONSTANT + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_ELECTRON_VOLT(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 1.602176634d-19 + value = ELECTRON_VOLT + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_ELEMENTARY_CHARGE(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 1.602176634d-19 + value = ELEMENTARY_CHARGE + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_FARADAY_CONSTANT(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 96485.33212d0 + value = FARADAY_CONSTANT + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_MOLAR_MASS_CONSTANT(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 0.99999999965d-3 + value = MOLAR_MASS_CONSTANT + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_MOLAR_VOLUME_NTP(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 22.41396954d-3 + value = MOLAR_VOLUME_OF_IDEAL_GAS__273_15_K__101_325_KPA + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_PLANCK_CONSTANT(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 6.62607015d-34 + value = PLANCK_CONSTANT + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_SPEED_OF_LIGHT(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected + expected = 299792458.0d0 + value = SPEED_OF_LIGHT_IN_VACUUM + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_STANDARD_ACCELERATION_OF_GRAVITY(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected + expected = 9.80665d0 + value = STANDARD_ACCELERATION_OF_GRAVITY + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +end module diff --git a/test/testsuite_uncertainties.f90 b/test/testsuite_uncertainties.f90 new file mode 100644 index 0000000..513799c --- /dev/null +++ b/test/testsuite_uncertainties.f90 @@ -0,0 +1,150 @@ +module testsuite_uncertainties + !! Test some uncertainty values. + use iso_fortran_env + use testdrive, only : new_unittest, unittest_type, error_type, check + use codata + implicit none + private + + public :: collect_suite_uncertainties + +contains + +subroutine collect_suite_uncertainties(testsuite) + !! Collection of tests + type(unittest_type), allocatable, intent(out) :: testsuite(:) + + testsuite = [& + new_unittest("U_ALPHA_PARTICLE_ELECTRON_MASS_RATIO", test_U_ALPHA_PARTICLE_ELECTRON_MASS_RATIO),& + new_unittest("U_ALPHA_PARTICLE_MASS", test_U_ALPHA_PARTICLE_MASS),& + new_unittest("U_ATOMIC_MASS_CONSTANT", test_U_ATOMIC_MASS_CONSTANT),& + new_unittest("U_AVOGADRO_CONSTANT", test_U_AVOGADRO_CONSTANT),& + new_unittest("U_BOLTZMANN_CONSTANT", test_U_BOLTZMANN_CONSTANT),& + new_unittest("U_ELECTRON_VOLT", test_U_ELECTRON_VOLT),& + new_unittest("U_ELEMENTARY_CHARGE", test_U_ELEMENTARY_CHARGE),& + new_unittest("U_FARADAY_CONSTANT", test_U_FARADAY_CONSTANT),& + new_unittest("U_MOLAR_MASS_CONSTANT", test_U_MOLAR_MASS_CONSTANT),& + new_unittest("U_MOLAR_VOLUME_OF_IDEAL_GAS__273_15K__101_325_KPA", test_U_MOLAR_VOLUME_NTP),& + new_unittest("U_PLANCK_CONSTANT", test_U_PLANCK_CONSTANT),& + new_unittest("U_SPEED_OF_LIGHT_IN_VACUUM", test_U_SPEED_OF_LIGHT),& + new_unittest("U_STANDARD_ACCELERATION_OF_GRAVITY", test_U_STANDARD_ACCELERATION_OF_GRAVITY)] +end subroutine + +subroutine test_U_ALPHA_PARTICLE_ELECTRON_MASS_RATIO(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 0.00000024d0 + value = U_ALPHA_PARTICLE_ELECTRON_MASS_RATIO + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_U_ALPHA_PARTICLE_MASS(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 0.0000000020d-27 + value = U_ALPHA_PARTICLE_MASS + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_U_ATOMIC_MASS_CONSTANT(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 0.00000000050d-27 + value = U_ATOMIC_MASS_CONSTANT + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_U_AVOGADRO_CONSTANT(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 0.0d0 + value = U_AVOGADRO_CONSTANT + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_U_BOLTZMANN_CONSTANT(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 0.0d0 + value = U_BOLTZMANN_CONSTANT + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_U_ELECTRON_VOLT(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 0.0d0 + value = U_ELECTRON_VOLT + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_U_ELEMENTARY_CHARGE(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 0.0d0 + value = U_ELEMENTARY_CHARGE + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_U_FARADAY_CONSTANT(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 0.0d0 + value = U_FARADAY_CONSTANT + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_U_MOLAR_MASS_CONSTANT(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 0.00000000030d-3 + value = U_MOLAR_MASS_CONSTANT + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_U_MOLAR_VOLUME_NTP(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 0.0d0 + value = U_MOLAR_VOLUME_OF_IDEAL_GAS__273_15_K__101_325_KPA + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_U_PLANCK_CONSTANT(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected, fac + expected = 0.0d0 + value = PLANCK_CONSTANT + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_U_SPEED_OF_LIGHT(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected + expected = 0.0d0 + value = U_SPEED_OF_LIGHT_IN_VACUUM + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +subroutine test_U_STANDARD_ACCELERATION_OF_GRAVITY(error) + type(error_type), allocatable, intent(out) :: error + real(real64) :: value, expected + expected = 0.0d0 + value = U_STANDARD_ACCELERATION_OF_GRAVITY + call check(error, value, expected) + if (allocated(error)) return +end subroutine + +end module diff --git a/test/testsuite_year.f90 b/test/testsuite_year.f90 new file mode 100644 index 0000000..04c424c --- /dev/null +++ b/test/testsuite_year.f90 @@ -0,0 +1,32 @@ +module testsuite_year + use iso_fortran_env + use testdrive, only : new_unittest, unittest_type, error_type, check + use codata + implicit none + private + + public :: collect_suite_year + +contains +!> Collect all exported unit tests +subroutine collect_suite_year(testsuite) + !> Collection of tests + type(unittest_type), allocatable, intent(out) :: testsuite(:) + + testsuite = [new_unittest("YEAR", test_year)] +end subroutine + +subroutine test_year(error) + type(error_type), allocatable, intent(out) :: error + + integer(int32) :: value, expected + + expected = 2018 + value = YEAR + + call check(error, value, expected) + if (allocated(error)) return +end subroutine + + +end module \ No newline at end of file