aboutsummaryrefslogtreecommitdiff
path: root/test/sca/test_process.py
blob: fda85754ad496590acfc53125a3e4a0428f8c1de (plain)
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
51
52
53
54
55
56
57
58
59
60
61
from unittest import TestCase

import numpy as np
from pyecsca.sca import (
    Trace,
    absolute,
    invert,
    threshold,
    rolling_mean,
    offset,
    recenter,
    normalize,
    normalize_wl,
)


class ProcessTests(TestCase):
    def setUp(self):
        self._trace = Trace(np.array([30, -60, 145, 247], dtype=np.dtype("i2")), None)

    def test_absolute(self):
        result = absolute(self._trace)
        self.assertIsNotNone(result)
        self.assertEqual(result.samples[1], 60)

    def test_invert(self):
        result = invert(self._trace)
        self.assertIsNotNone(result)
        np.testing.assert_equal(result.samples, [-30, 60, -145, -247])

    def test_threshold(self):
        result = threshold(self._trace, 128)
        self.assertIsNotNone(result)
        self.assertEqual(result.samples[0], 0)
        self.assertEqual(result.samples[2], 1)

    def test_rolling_mean(self):
        result = rolling_mean(self._trace, 2)
        self.assertIsNotNone(result)
        self.assertEqual(len(result.samples), 3)
        self.assertEqual(result.samples[0], -15)
        self.assertEqual(result.samples[1], 42)
        self.assertEqual(result.samples[2], 196)

    def test_offset(self):
        result = offset(self._trace, 5)
        self.assertIsNotNone(result)
        np.testing.assert_equal(
            result.samples, np.array([35, -55, 150, 252], dtype=np.dtype("i2"))
        )

    def test_recenter(self):
        self.assertIsNotNone(recenter(self._trace))

    def test_normalize(self):
        result = normalize(self._trace)
        self.assertIsNotNone(result)

    def test_normalize_wl(self):
        result = normalize_wl(self._trace)
        self.assertIsNotNone(result)