test.py 2.6 KB
Newer Older
J
Jeff Wang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2017 VisualDL Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =======================================================================

S
superjom 已提交
16
import sys
T
Thuan Nguyen 已提交
17

S
superjom 已提交
18
import unittest
S
superjom 已提交
19
import numpy as np
S
superjom 已提交
20

T
Thuan Nguyen 已提交
21
sys.path.append('../../build')  # noqa: E402
S
superjom 已提交
22 23 24 25
import core

im = core.im()

S
superjom 已提交
26

S
superjom 已提交
27 28
class StorageTester(unittest.TestCase):
    def setUp(self):
S
superjom 已提交
29
        im.clear_tablets()
S
superjom 已提交
30 31 32 33 34 35 36 37 38 39 40 41
        self.storage = im.storage()

    def test_size(self):
        self.assertEqual(self.storage.tablets_size(), 0)
        im.add_tablet("tag0", 100)
        self.assertEqual(self.storage.tablets_size(), 1)

        for i in range(1, 11):
            im.add_tablet("tag%d" % i, 100)
        self.assertEqual(self.storage.tablets_size(), 11)

    def test_timestamp(self):
O
Oraoto 已提交
42
        print(self.storage.timestamp())
S
superjom 已提交
43 44 45 46 47 48 49

    def test_dir(self):
        dir = "./1.txt"
        self.storage.set_dir(dir)
        self.assertEqual(dir, self.storage.dir())

    def test_human_readable_buffer(self):
O
Oraoto 已提交
50
        print(self.storage.human_readable_buffer())
S
superjom 已提交
51 52 53 54


class TabletTester(unittest.TestCase):
    def setUp(self):
S
superjom 已提交
55
        im.clear_tablets()
S
superjom 已提交
56 57 58
        self.tablet = im.add_tablet("tag101", 20)

    def test_human_readable_buffer(self):
O
Oraoto 已提交
59
        print(self.tablet.human_readable_buffer())
S
superjom 已提交
60

S
superjom 已提交
61 62 63 64
    def test_scalar(self):
        scalar = self.tablet.as_float_scalar()
        py_captions = ["train", "test"]
        step_ids = [10, 20, 30]
Q
Qiao Longfei 已提交
65
        py_records = [[0.1, 0.2], [0.2, 0.3], [0.3, 0.4]]
S
superjom 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

        scalar.set_captions(py_captions)
        for i in range(len(py_records)):
            scalar.add_record(step_ids[i], py_records[i])

        records = scalar.get_records()
        ids = scalar.get_ids()
        for i in range(len(py_records)):
            self.assertTrue(np.isclose(py_records[i], records[i]).all())
            self.assertEqual(step_ids[i], ids[i])


class ImTester(unittest.TestCase):
    def test_persist(self):
        im.clear_tablets()
T
Thuan Nguyen 已提交
81
        im.add_tablet("tab0", 111)
S
superjom 已提交
82 83 84 85
        self.assertEqual(im.storage().tablets_size(), 1)
        im.storage().set_dir("./1")
        im.persist_to_disk()

S
superjom 已提交
86 87 88

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