File: test-nist-compounds.py

package info (click to toggle)
xraylib 4.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 46,976 kB
  • sloc: ansic: 16,363; f90: 9,202; java: 6,998; python: 1,580; cpp: 1,317; pascal: 1,139; makefile: 810; ruby: 622; php: 594; perl: 573; cs: 193; sh: 160
file content (50 lines) | stat: -rw-r--r-- 2,153 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
if "EXTRA_DLL_SEARCH_PATHS" in os.environ and hasattr(os, "add_dll_directory"):
    for path in os.environ["EXTRA_DLL_SEARCH_PATHS"].split(os.pathsep):
        os.add_dll_directory(path)
import unittest
import xraylib


class TestNISTCompounds(unittest.TestCase):
    def test_good(self):
        list = xraylib.GetCompoundDataNISTList()
        self.assertEqual(len(list), 180)
        for i, v in enumerate(list):
            cdn = xraylib.GetCompoundDataNISTByIndex(i)
            self.assertEqual(cdn['name'], v)
            cdn = xraylib.GetCompoundDataNISTByName(v)
            self.assertEqual(cdn['name'], v)

        cdn = xraylib.GetCompoundDataNISTByIndex(5)
        self.assertEqual(cdn['nElements'], 4)
        self.assertEqual(cdn['density'], 0.001205)
        self.assertEqual(cdn['Elements'], (6, 7, 8, 18))
        self.assertAlmostEqual(cdn['massFractions'], (0.000124, 0.755267, 0.231781, 0.012827))
        self.assertEqual(cdn['name'], 'Air, Dry (near sea level)')

        cdn = xraylib.GetCompoundDataNISTByName('Air, Dry (near sea level)')
        self.assertEqual(cdn['nElements'], 4)
        self.assertEqual(cdn['density'], 0.001205)
        self.assertEqual(cdn['Elements'], (6, 7, 8, 18))
        self.assertAlmostEqual(cdn['massFractions'], (0.000124, 0.755267, 0.231781, 0.012827))
        self.assertEqual(cdn['name'], 'Air, Dry (near sea level)')

    def test_bad(self):
        with self.assertRaises(ValueError):
            xraylib.GetCompoundDataNISTByName("jwjfpfj")
        with self.assertRaises(TypeError):
            xraylib.GetCompoundDataNISTByName(0)
        with self.assertRaises(ValueError):
            xraylib.GetCompoundDataNISTByName(None)
        with self.assertRaises(ValueError):
            xraylib.GetCompoundDataNISTByIndex(-1)
        with self.assertRaises(ValueError):
            xraylib.GetCompoundDataNISTByIndex(180)
        with self.assertRaises(TypeError):
            xraylib.GetCompoundDataNISTByIndex(None)
        with self.assertRaises(TypeError):
            xraylib.GetCompoundDataNISTByIndex("jpwjfpfwj")

if __name__ == '__main__':
    unittest.main(verbosity=2)