File: test_json_arrays.py

package info (click to toggle)
python-ase 3.26.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,484 kB
  • sloc: python: 148,112; xml: 2,728; makefile: 110; javascript: 47
file content (27 lines) | stat: -rw-r--r-- 739 bytes parent folder | download
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
# fmt: off
import numpy as np

from ase.io.jsonio import decode, encode


def test_json_arrays():

    def check(obj):
        txt = encode(obj)
        newobj = decode(txt, always_array=False)
        print(obj, '-->', newobj)
        assert isinstance(obj, type(newobj)), '{} vs {}'.format(type(obj),
                                                                type(newobj))
        assert np.shape(obj) == np.shape(newobj)
        assert np.array_equal(obj, newobj)

    check([1, 2, 3])
    check([1.0, 2.0, 3.0])
    check([])

    check(np.arange(3))
    check(np.arange(3).astype(float))
    check(np.empty((3, 0, 7)))
    check(np.empty((0, 3, 7), dtype=int))
    check(np.ones(2, complex))
    check(np.ones(2, np.complex64))