1 /*
2  * Copyright (c) 2019 Kevin Townsend (KTOWN)
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <zsl/zsl.h>
9 #include <zsl/matrices.h>
10 #include <zsl/vectors.h>
11 #include <zsl/physics/photons.h>
12 #include "floatcheck.h"
13 
ZTEST(zsl_tests,test_phy_photon_ener)14 ZTEST(zsl_tests, test_phy_photon_ener)
15 {
16 	int rc;
17 	zsl_real_t e;
18 
19 	/* Green light: wavelength = 550 nanometers = 5.5e-7 meters. */
20 	rc = zsl_phy_photon_ener(5.5E-7, &e);
21 	zassert_true(rc == 0, NULL);
22 	zassert_true(val_is_equal(e, 2.2542581533, 1E-6), NULL);
23 
24 	/* Example for negative wavelength. */
25 	rc = zsl_phy_photon_ener(-5.5E-7, &e);
26 	zassert_true(rc == -EINVAL, NULL);
27 	/* IEEE standard states that x != x is true only for NAN values. */
28 	zassert_true(e != e, NULL);
29 
30 	/* Example for zero wavelength. */
31 	rc = zsl_phy_photon_ener(0.0, &e);
32 	zassert_true(rc == -EINVAL, NULL);
33 	/* IEEE standard states that x != x is true only for NAN values. */
34 	zassert_true(e != e, NULL);
35 }
36 
ZTEST(zsl_tests,test_phy_photon_mom)37 ZTEST(zsl_tests, test_phy_photon_mom)
38 {
39 	int rc;
40 	zsl_real_t p;
41 
42 	rc = zsl_phy_photon_mom(5.5E-12, &p);
43 	zassert_true(rc == 0, NULL);
44 	zassert_true(val_is_equal(p, 0.0007519396, 1E-6), NULL);
45 
46 	/* Example for negative wavelength. */
47 	rc = zsl_phy_photon_mom(-5.5E-12, &p);
48 	zassert_true(rc == -EINVAL, NULL);
49 	/* IEEE standard states that x != x is true only for NAN values. */
50 	zassert_true(p != p, NULL);
51 
52 	/* Example for zero wavelength. */
53 	rc = zsl_phy_photon_mom(0.0, &p);
54 	zassert_true(rc == -EINVAL, NULL);
55 	/* IEEE standard states that x != x is true only for NAN values. */
56 	zassert_true(p != p, NULL);
57 }
58 
ZTEST(zsl_tests,test_phy_photon_wavelenght)59 ZTEST(zsl_tests, test_phy_photon_wavelenght)
60 {
61 	int rc;
62 	zsl_real_t lambda;
63 
64 	rc = zsl_phy_photon_wavelength(5.5E8, &lambda);
65 	zassert_true(rc == 0, NULL);
66 	zassert_true(val_is_equal(lambda, 0.5450771964, 1E-6), NULL);
67 
68 	/* Example for negative frequency. */
69 	rc = zsl_phy_photon_wavelength(-5.5E8, &lambda);
70 	zassert_true(rc == -EINVAL, NULL);
71 	/* IEEE standard states that x != x is true only for NAN values. */
72 	zassert_true(lambda != lambda, NULL);
73 
74 	/* Example for zero frequency. */
75 	rc = zsl_phy_photon_wavelength(0.0, &lambda);
76 	zassert_true(rc == -EINVAL, NULL);
77 	/* IEEE standard states that x != x is true only for NAN values. */
78 	zassert_true(lambda != lambda, NULL);
79 }
80 
ZTEST(zsl_tests,test_phy_photon_frequency)81 ZTEST(zsl_tests, test_phy_photon_frequency)
82 {
83 	int rc;
84 	zsl_real_t f;
85 
86 	rc = zsl_phy_photon_frequency(5.5E4, &f);
87 	zassert_true(rc == 0, NULL);
88 	zassert_true(val_is_equal(f, 5450.7719636363, 1E-6), NULL);
89 
90 	/* Example for negative wavelength. */
91 	rc = zsl_phy_photon_frequency(-5.5E8, &f);
92 	zassert_true(rc == -EINVAL, NULL);
93 	/* IEEE standard states that x != x is true only for NAN values. */
94 	zassert_true(f != f, NULL);
95 
96 	/* Example for zero wavelength. */
97 	rc = zsl_phy_photon_frequency(0.0, &f);
98 	zassert_true(rc == -EINVAL, NULL);
99 	/* IEEE standard states that x != x is true only for NAN values. */
100 	zassert_true(f != f, NULL);
101 }
102 
ZTEST(zsl_tests,test_phy_photon_photoelectric)103 ZTEST(zsl_tests, test_phy_photon_photoelectric)
104 {
105 	int rc;
106 	zsl_real_t kmax;
107 
108 	rc = zsl_phy_photon_photoelectric(5.5E14, 1.3, &kmax);
109 	zassert_true(rc == 0, NULL);
110 	zassert_true(val_is_equal(kmax, 0.9746172333, 1E-6), NULL);
111 
112 	/* Example for negative frequency. */
113 	rc = zsl_phy_photon_photoelectric(-5.5E14, 1.3, &kmax);
114 	zassert_true(rc == -EINVAL, NULL);
115 	/* IEEE standard states that x != x is true only for NAN values. */
116 	zassert_true(kmax != kmax, NULL);
117 
118 	/* Example for negative work function. */
119 	rc = zsl_phy_photon_photoelectric(5.5E14, -1.3, &kmax);
120 	zassert_true(rc == -EINVAL, NULL);
121 	/* IEEE standard states that x != x is true only for NAN values. */
122 	zassert_true(kmax != kmax, NULL);
123 }
124