test.py 1.9 KB
Newer Older
S
superjom 已提交
1 2
import sys
import unittest
S
superjom 已提交
3
import numpy as np
S
superjom 已提交
4

S
superjom 已提交
5 6 7 8 9
sys.path.append('../../build')
import core

im = core.im()

S
superjom 已提交
10

S
superjom 已提交
11 12
class StorageTester(unittest.TestCase):
    def setUp(self):
S
superjom 已提交
13
        im.clear_tablets()
S
superjom 已提交
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
        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):
        print self.storage.timestamp()

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

    def test_human_readable_buffer(self):
        print self.storage.human_readable_buffer()


class TabletTester(unittest.TestCase):
    def setUp(self):
S
superjom 已提交
39
        im.clear_tablets()
S
superjom 已提交
40 41 42 43 44
        self.tablet = im.add_tablet("tag101", 20)

    def test_human_readable_buffer(self):
        print self.tablet.human_readable_buffer()

S
superjom 已提交
45 46 47 48
    def test_scalar(self):
        scalar = self.tablet.as_float_scalar()
        py_captions = ["train", "test"]
        step_ids = [10, 20, 30]
Q
Qiao Longfei 已提交
49
        py_records = [[0.1, 0.2], [0.2, 0.3], [0.3, 0.4]]
S
superjom 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

        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()
        tablet = im.add_tablet("tab0", 111)
        self.assertEqual(im.storage().tablets_size(), 1)
        im.storage().set_dir("./1")
        im.persist_to_disk()

S
superjom 已提交
70 71 72

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