|
33 | 33 | funcs{end+1} = @test_has_section;
|
34 | 34 | funcs{end+1} = @test_filter_section;
|
35 | 35 | funcs{end+1} = @test_filter_block;
|
| 36 | + funcs{end+1} = @test_find_section; |
| 37 | + funcs{end+1} = @test_find_section_filtered; |
36 | 38 | end
|
37 | 39 |
|
38 | 40 | %% Test: Open HDF5 file in ReadOnly mode
|
|
411 | 413 | end
|
412 | 414 |
|
413 | 415 | end
|
| 416 | + |
| 417 | +%% Test: Find sections w/o filter |
| 418 | +function [] = test_find_section |
| 419 | + f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite); |
| 420 | + sl1 = f.create_section('sectionLvl1', 'nixSection'); |
| 421 | + |
| 422 | + sl21 = sl1.create_section('sectionLvl2_1', 'nixSection'); |
| 423 | + sl22 = sl1.create_section('sectionLvl2_2', 'nixSection'); |
| 424 | + |
| 425 | + sl31 = sl21.create_section('sectionLvl3_1', 'nixSection'); |
| 426 | + sl32 = sl21.create_section('sectionLvl3_2', 'nixSection'); |
| 427 | + sl33 = sl21.create_section('sectionLvl3_3', 'nixSection'); |
| 428 | + |
| 429 | + sl41 = sl31.create_section('sectionLvl4_1', 'nixSection'); |
| 430 | + sl42 = sl31.create_section('sectionLvl4_2', 'nixSection'); |
| 431 | + sl43 = sl31.create_section('sectionLvl4_3', 'nixSection'); |
| 432 | + sl44 = sl31.create_section('sectionLvl4_4', 'nixSection'); |
| 433 | + |
| 434 | + % Check invalid entry |
| 435 | + err = 'Provide a valid search depth'; |
| 436 | + try |
| 437 | + f.find_sections('hurra'); |
| 438 | + catch ME |
| 439 | + assert(strcmp(ME.message, err)); |
| 440 | + end |
| 441 | + |
| 442 | + % find all |
| 443 | + filtered = f.find_sections(4); |
| 444 | + assert(size(filtered, 1) == 10); |
| 445 | + |
| 446 | + % find until level 4 |
| 447 | + filtered = f.find_sections(3); |
| 448 | + assert(size(filtered, 1) == 10); |
| 449 | + |
| 450 | + % find until level 3 |
| 451 | + filtered = f.find_sections(2); |
| 452 | + assert(size(filtered, 1) == 6); |
| 453 | + |
| 454 | + % find until level 2 |
| 455 | + filtered = f.find_sections(1); |
| 456 | + assert(size(filtered, 1) == 3); |
| 457 | + |
| 458 | + % find until level 1 |
| 459 | + filtered = f.find_sections(0); |
| 460 | + assert(size(filtered, 1) == 1); |
| 461 | +end |
| 462 | + |
| 463 | +%% Test: Find sections with filter |
| 464 | +function [] = test_find_section_filtered |
| 465 | + findSection = 'nixFindSection'; |
| 466 | + f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite); |
| 467 | + sl1 = f.create_section('sectionLvl1', 'nixSection'); |
| 468 | + |
| 469 | + sl21 = sl1.create_section('sectionLvl2_1', 'nixSection'); |
| 470 | + sl22 = sl1.create_section('sectionLvl2_2', findSection); |
| 471 | + |
| 472 | + sl31 = sl21.create_section('sectionLvl3_1', 'nixSection'); |
| 473 | + sl32 = sl21.create_section('sectionLvl3_2', 'nixSection'); |
| 474 | + sl33 = sl21.create_section('sectionLvl3_3', findSection); |
| 475 | + |
| 476 | + sl41 = sl31.create_section('sectionLvl4_1', 'nixSection'); |
| 477 | + sl42 = sl31.create_section('sectionLvl4_2', 'nixSection'); |
| 478 | + sl43 = sl31.create_section('sectionLvl4_3', findSection); |
| 479 | + sl44 = sl31.create_section('sectionLvl4_4', 'nixSection'); |
| 480 | + |
| 481 | + % test find by id |
| 482 | + filtered = f.find_filtered_sections(0, nix.Filter.id, sl41.id); |
| 483 | + assert(isempty(filtered)); |
| 484 | + filtered = f.find_filtered_sections(3, nix.Filter.id, sl41.id); |
| 485 | + assert(size(filtered, 1) == 1); |
| 486 | + assert(strcmp(filtered{1}.id, sl41.id)); |
| 487 | + |
| 488 | + % test find by ids |
| 489 | + filterids = {sl1.id, sl41.id}; |
| 490 | + filtered = f.find_filtered_sections(0, nix.Filter.ids, filterids); |
| 491 | + assert(size(filtered, 1) == 1); |
| 492 | + filtered = f.find_filtered_sections(3, nix.Filter.ids, filterids); |
| 493 | + assert(size(filtered, 1) == 2); |
| 494 | + |
| 495 | + % test find by name |
| 496 | + filtered = f.find_filtered_sections(0, nix.Filter.name, sl41.name); |
| 497 | + assert(isempty(filtered)); |
| 498 | + filtered = f.find_filtered_sections(3, nix.Filter.name, sl41.name); |
| 499 | + assert(size(filtered, 1) == 1); |
| 500 | + assert(strcmp(filtered{1}.name, sl41.name)); |
| 501 | + |
| 502 | + % test find by type |
| 503 | + filtered = f.find_filtered_sections(0, nix.Filter.type, findSection); |
| 504 | + assert(isempty(filtered)); |
| 505 | + filtered = f.find_filtered_sections(3, nix.Filter.type, findSection); |
| 506 | + assert(size(filtered, 1) == 3); |
| 507 | + assert(strcmp(filtered{1}.type, findSection)); |
| 508 | + |
| 509 | + % test fail on nix.Filter.metadata |
| 510 | + err = 'unknown or unsupported filter'; |
| 511 | + try |
| 512 | + f.find_filtered_sections(1, nix.Filter.metadata, 'metadata'); |
| 513 | + catch ME |
| 514 | + assert(strcmp(ME.message, err)); |
| 515 | + end |
| 516 | + |
| 517 | + % test fail on nix.Filter.source |
| 518 | + try |
| 519 | + f.find_filtered_sections(1, nix.Filter.source, 'source'); |
| 520 | + catch ME |
| 521 | + assert(strcmp(ME.message, err)); |
| 522 | + end |
| 523 | +end |
0 commit comments