|
| 1 | +# -*- coding:utf-8 -*- |
| 2 | +''' |
| 3 | +Unit tests for vaspy.functions module. |
| 4 | +''' |
| 5 | + |
| 6 | +import unittest |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +from ..functions import (str2list, line2list, array2str, |
| 10 | + combine_atomco_dict, atomdict2str, |
| 11 | + get_combinations, get_angle) |
| 12 | + |
| 13 | + |
| 14 | +class Str2listTest(unittest.TestCase): |
| 15 | + |
| 16 | + def test_str2list(self): |
| 17 | + result = str2list(' 1.0 2.0 3.0 ') |
| 18 | + self.assertListEqual(result, ['1.0', '2.0', '3.0']) |
| 19 | + |
| 20 | + def test_str2list_empty(self): |
| 21 | + result = str2list('') |
| 22 | + self.assertEqual(result, []) |
| 23 | + |
| 24 | + |
| 25 | +class Line2listTest(unittest.TestCase): |
| 26 | + |
| 27 | + def test_line2list_float(self): |
| 28 | + result = line2list('1.0 2.0 3.0', dtype=float) |
| 29 | + self.assertListEqual(result, [1.0, 2.0, 3.0]) |
| 30 | + |
| 31 | + def test_line2list_int(self): |
| 32 | + result = line2list('10 20 30', dtype=int) |
| 33 | + self.assertListEqual(result, [10, 20, 30]) |
| 34 | + |
| 35 | + def test_line2list_str(self): |
| 36 | + result = line2list('a b c', dtype=str) |
| 37 | + self.assertListEqual(result, ['a', 'b', 'c']) |
| 38 | + |
| 39 | + def test_line2list_custom_field(self): |
| 40 | + result = line2list('1.0,2.0,3.0', field=',', dtype=float) |
| 41 | + self.assertListEqual(result, [1.0, 2.0, 3.0]) |
| 42 | + |
| 43 | + def test_line2list_empty_elements(self): |
| 44 | + result = line2list(' 1.0 2.0 ', dtype=float) |
| 45 | + self.assertListEqual(result, [1.0, 2.0]) |
| 46 | + |
| 47 | + def test_line2list_type_error(self): |
| 48 | + with self.assertRaises(TypeError): |
| 49 | + line2list('1.0 2.0', dtype=3.14) |
| 50 | + |
| 51 | + |
| 52 | +class Array2strTest(unittest.TestCase): |
| 53 | + |
| 54 | + def test_array2str(self): |
| 55 | + arr = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) |
| 56 | + result = array2str(arr) |
| 57 | + self.assertIn('1.0000000000000000', result) |
| 58 | + self.assertIn('2.0000000000000000', result) |
| 59 | + self.assertEqual(result.count('\n'), 2) |
| 60 | + |
| 61 | + |
| 62 | +class CombineAtomcoDictTest(unittest.TestCase): |
| 63 | + |
| 64 | + def test_combine_disjoint(self): |
| 65 | + a = {'C': [[1.0, 2.0, 3.0]]} |
| 66 | + b = {'O': [[4.0, 5.0, 6.0]]} |
| 67 | + result = combine_atomco_dict(a, b) |
| 68 | + self.assertEqual(set(result.keys()), {'C', 'O'}) |
| 69 | + |
| 70 | + def test_combine_overlap(self): |
| 71 | + a = {'C': [[1.0, 2.0, 3.0]]} |
| 72 | + b = {'C': [[4.0, 5.0, 6.0]]} |
| 73 | + result = combine_atomco_dict(a, b) |
| 74 | + self.assertEqual(len(result['C']), 2) |
| 75 | + |
| 76 | + def test_combine_empty(self): |
| 77 | + result = combine_atomco_dict({}, {}) |
| 78 | + self.assertEqual(result, {}) |
| 79 | + |
| 80 | + |
| 81 | +class Atomdict2strTest(unittest.TestCase): |
| 82 | + |
| 83 | + def test_atomdict2str(self): |
| 84 | + d = {'C': [[2.01115823704755, 2.33265069974919, 10.54948252493041]], |
| 85 | + 'Co': [[0.28355818414485, 2.31976779057375, 2.34330019781397], |
| 86 | + [2.76900337448991, 0.88479534087197, 2.34330019781397]]} |
| 87 | + result = atomdict2str(d, ['C', 'Co']) |
| 88 | + self.assertIn('C', result) |
| 89 | + self.assertIn('Co', result) |
| 90 | + self.assertEqual(result.count('\n'), 3) |
| 91 | + |
| 92 | + |
| 93 | +class GetCombinationsTest(unittest.TestCase): |
| 94 | + |
| 95 | + def test_get_combinations(self): |
| 96 | + result = get_combinations(3, 4, 5) |
| 97 | + self.assertIsInstance(result, np.ndarray) |
| 98 | + |
| 99 | + |
| 100 | +class GetAngleTest(unittest.TestCase): |
| 101 | + |
| 102 | + def test_get_angle_90(self): |
| 103 | + v1 = np.array([1.0, 0.0, 0.0]) |
| 104 | + v2 = np.array([0.0, 1.0, 0.0]) |
| 105 | + self.assertAlmostEqual(get_angle(v1, v2), 90.0) |
| 106 | + |
| 107 | + def test_get_angle_0(self): |
| 108 | + v1 = np.array([1.0, 0.0, 0.0]) |
| 109 | + self.assertAlmostEqual(get_angle(v1, v1), 0.0) |
| 110 | + |
| 111 | + def test_get_angle_180(self): |
| 112 | + v1 = np.array([1.0, 0.0, 0.0]) |
| 113 | + v2 = np.array([-1.0, 0.0, 0.0]) |
| 114 | + self.assertAlmostEqual(get_angle(v1, v2), 180.0) |
0 commit comments