def test_08_01_reopen(self): strings = ["foo", "bar"] a = H5DICT.VStringArray(self.hdf_file) strings = ["foo", "bar"] a.set_all(strings) b = H5DICT.VStringArray(self.hdf_file) for s0, s1 in zip(b, strings): self.assertEqual(s0, s1)
def test_02_01_set_one(self): a = H5DICT.VStringArray(self.hdf_file) a[0] = "foo" self.assertEqual(self.hdf_file["index"].shape[0], 1) self.assertEqual(self.hdf_file["index"][0, 0], 0) self.assertEqual(self.hdf_file["index"][0, 1], 3) self.assertEqual(self.hdf_file["data"][:].tostring(), "foo")
def test_04_05_get_two(self): a = H5DICT.VStringArray(self.hdf_file) strings = ["foo", "bar"] for i, s in enumerate(strings): a[i] = s for i, s in enumerate(strings): self.assertEqual(s, a[i])
def test_13_01_reorder(self): data = ["hello", "green", "world"] order = [2, 0, 1] a = H5DICT.VStringArray(self.hdf_file) a.set_all(data) a.reorder(order) self.assertSequenceEqual([data[o] for o in order], a)
def test_12_02_extend_many(self): data = ["hello", "world"] extend = ["that", "is", "observing", "the", "transit", "of", "venus"] a = H5DICT.VStringArray(self.hdf_file) a.set_all(data) a.extend(extend) self.assertSequenceEqual(data + extend, a)
def test_12_01_extend_one(self): data = ["hello", "world"] extend = ["."] a = H5DICT.VStringArray(self.hdf_file) a.set_all(data) a.extend(extend) self.assertSequenceEqual(data + extend, a)
def test_05_03_del_not_last(self): a = H5DICT.VStringArray(self.hdf_file) a[0] = "foo" a[1] = "bar" del a[0] self.assertEqual(len(a), 1) self.assertEqual(a[0], "bar")
def test_07_03_set_all_with_nulls(self): a = H5DICT.VStringArray(self.hdf_file) strings = ["foo", None, "bar"] a.set_all(strings) self.assertEqual(len(a), len(strings)) for s0, s1 in zip(a, strings): self.assertEqual(s0, s1)
def test_06_01_iter(self): a = H5DICT.VStringArray(self.hdf_file) strings = ["foo", "bar"] for i, s in enumerate(strings): a[i] = s for s0, s1 in zip(a, strings): self.assertEqual(s0, s1)
def test_13_02_reorder_with_delete(self): data = ["hello", "green", "world"] order = [2, 0] a = H5DICT.VStringArray(self.hdf_file) a.set_all(data) a.reorder(order) self.assertEqual(len(a), len(order)) self.assertSequenceEqual([data[o] for o in order], a)
def test_11_01_bisect_left(self): a = H5DICT.VStringArray(self.hdf_file) r = np.random.RandomState() r.seed(1100) lengths = r.randint(3, 10, 100) idx = np.hstack([[0], np.cumsum(lengths)]) chars = r.randint(ord('A'), ord('F') + 1, idx[-1]).astype(np.uint8) strings = [chars[i:j].tostring() for i, j in zip(idx[:-1], idx[1:])] a = H5DICT.VStringArray(self.hdf_file) a.set_all(strings[:50]) a.sort() for s in strings[50:]: idx = a.bisect_left(s) if idx > 0: self.assertLessEqual(a[idx - 1], s) if idx < len(a): self.assertGreaterEqual(a[idx], s)
def test_10_01_insert(self): a = H5DICT.VStringArray(self.hdf_file) a.set_all(["hello", "world"]) a.insert(2, ".") a.insert(1, "brave") a.insert(2, "new") for s0, s1 in zip(["hello", "brave", "new", "world", "."], a): self.assertEqual(s0, s1)
def test_09_03_sort_none(self): a = H5DICT.VStringArray(self.hdf_file) a.set_all(["hello", None, "world", None]) a.sort() self.assertIsNone(a[0]) self.assertIsNone(a[1]) self.assertEqual(a[2], "hello") self.assertEqual(a[3], "world")
def test_07_02_set_all_dirty(self): a = H5DICT.VStringArray(self.hdf_file) a.set_all(["blah", "blah", "blah", "blah"]) strings = ["foo", "bar"] a.set_all(strings) self.assertEqual(len(a), len(strings)) for s0, s1 in zip(a, strings): self.assertEqual(s0, s1)
def test_02_04_set_and_set_longer(self): a = H5DICT.VStringArray(self.hdf_file) a[0] = "foo" a[0] = "whoops" self.assertEqual(self.hdf_file["index"].shape[0], 1) self.assertEqual( self.hdf_file["index"][0, 1] - self.hdf_file["index"][0, 0], 6) self.assertEqual( self.hdf_file["data"][self.hdf_file["index"][ 0, 0]:self.hdf_file["index"][0, 1]].tostring(), "whoops")
def test_02_03_set_and_set_shorter(self): a = H5DICT.VStringArray(self.hdf_file) a[0] = "foo" a[0] = "hu" self.assertEqual(self.hdf_file["index"].shape[0], 1) self.assertEqual( self.hdf_file["index"][0, 1] - self.hdf_file["index"][0, 0], 2) self.assertEqual( self.hdf_file["data"][self.hdf_file["index"][ 0, 0]:self.hdf_file["index"][0, 1]].tostring(), "hu")
def test_03_07_add_a_file_to_the_schema(self): for filelist, cache in ((self.filelist, True), (self.filelist_nocache, False)): g = filelist.get_filelist_group() filelist.add_files_to_filelist(["file:foo.jpg"]) if not cache: filelist.clear_cache() self.assertIn(E("file"), g) filenames = list(H5DICT.VStringArray(g[E("file")])) self.assertEqual(len(filenames), 1) self.assertEqual(filenames[0], "foo.jpg")
def test_09_02_sort(self): r = np.random.RandomState() r.seed(92) lengths = r.randint(3, 10, 100) idx = np.hstack([[0], np.cumsum(lengths)]) chars = r.randint(ord('A'), ord('F') + 1, idx[-1]).astype(np.uint8) strings = [chars[i:j].tostring() for i, j in zip(idx[:-1], idx[1:])] a = H5DICT.VStringArray(self.hdf_file) a.set_all(strings) a.sort() for s0, s1 in zip(sorted(strings), a): self.assertEqual(s0, s1)
def test_03_01_add_file(self): for filelist, cache in ((self.filelist, True), (self.filelist_nocache, False)): g = filelist.get_filelist_group() filelist.add_files_to_filelist(["file://foo/bar.jpg"]) if not cache: filelist.clear_cache() self.assertIn(E("file"), g) self.assertIn(E("//foo"), g["file"]) filenames = H5DICT.VStringArray(g[E("file")][E("//foo")]) self.assertEqual(len(filenames), 1) self.assertIn("bar.jpg", filenames)
def test_03_02_add_two_files(self): for filelist, cache in ((self.filelist, True), (self.filelist_nocache, False)): g = filelist.get_filelist_group() if not cache: filelist.clear_cache() filelist.add_files_to_filelist( ["file://foo/bar.jpg", "file://foo/baz.jpg"]) self.assertIn(E("file"), g) self.assertIn(E("//foo"), g[E("file")]) filenames = list(H5DICT.VStringArray(g[E("file")][E("//foo")])) self.assertEqual(len(filenames), 2) self.assertEqual(filenames[0], "bar.jpg") self.assertEqual(filenames[1], "baz.jpg")
def test_03_05_add_a_file_with_a_stupid_DOS_name(self): for filelist, cache in ((self.filelist, True), (self.filelist_nocache, False)): g = filelist.get_filelist_group() filelist.add_files_to_filelist(["file:///C:/foo/bar.jpg"]) if not cache: filelist.clear_cache() self.assertIn(E("file"), g) self.assertIn(E("///C:"), g[E("file")]) self.assertIn(E("foo"), g[E("file")][E("///C:")]) filenames = list( H5DICT.VStringArray(g[E("file")][E("///C:")][E("foo")])) self.assertEqual(len(filenames), 1) self.assertEqual(filenames[0], "bar.jpg")
def test_03_03_add_two_directories(self): g = self.filelist.get_filelist_group() for filelist, cache in ((self.filelist, True), (self.filelist_nocache, False)): filelist.add_files_to_filelist( ["file://foo/bar.jpg", "file://bar/baz.jpg"]) if not cache: filelist.clear_cache() for subdir, filename in (("//foo", "bar.jpg"), ("//bar", "baz.jpg")): self.assertIn(E("file"), g) self.assertIn(E(subdir), g[E("file")]) filenames = list(H5DICT.VStringArray(g[E("file")][E(subdir)])) self.assertEqual(len(filenames), 1) self.assertEqual(filenames[0], filename)
def test_04_03_remove_all_files_in_parent(self): for filelist, cache in ((self.filelist, True), (self.filelist_nocache, False)): g = filelist.get_filelist_group() filelist.add_files_to_filelist( ["file://foo/bar.jpg", "file:baz.jpg"]) if not cache: filelist.clear_cache() self.assertTrue(H5DICT.VStringArray.has_vstring_array( g[E("file")])) filelist.remove_files_from_filelist(["file:baz.jpg"]) if not cache: filelist.clear_cache() self.assertIn(E("file"), g) self.assertIn(E("//foo"), g[E("file")]) filenames = list(H5DICT.VStringArray(g[E("file")][E("//foo")])) self.assertEqual(len(filenames), 1) self.assertIn("bar.jpg", filenames) self.assertFalse( H5DICT.VStringArray.has_vstring_array(g[E("file")]))
def test_05_01_del_one(self): a = H5DICT.VStringArray(self.hdf_file) a[0] = "foo" del a[0] self.assertEqual(len(a), 0)
def test_04_04_get_unicode(self): s = u"\u03b4x" a = H5DICT.VStringArray(self.hdf_file) a[0] = s self.assertEqual(a[0], s)
def test_04_03_get_zero_len(self): a = H5DICT.VStringArray(self.hdf_file) a[0] = "" self.assertEqual(a[0], "")
def test_04_02_get_null(self): a = H5DICT.VStringArray(self.hdf_file) a[0] = None self.assertIsNone(a[0])
def test_04_01_get(self): a = H5DICT.VStringArray(self.hdf_file) a[0] = "foo" self.assertEqual(a[0], "foo")
def test_03_01_len(self): a = H5DICT.VStringArray(self.hdf_file) a[0] = "foo" a[1] = "bar" self.assertEqual(len(a), 2)
def test_02_05_set_empty(self): a = H5DICT.VStringArray(self.hdf_file) a[0] = "" self.assertEqual(self.hdf_file["index"].shape[0], 1) self.assertEqual(self.hdf_file["index"][0, 0], 0) self.assertEqual(self.hdf_file["index"][0, 1], 0)