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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
|
package test.object.misc;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Array;
import java.util.List;
import java.util.Vector;
import ncsa.hdf.hdf5lib.H5;
import ncsa.hdf.hdf5lib.HDF5Constants;
import ncsa.hdf.hdf5lib.exceptions.HDF5Exception;
import ncsa.hdf.hdf5lib.exceptions.HDF5LibraryException;
import ncsa.hdf.object.Attribute;
import ncsa.hdf.object.CompoundDS;
import ncsa.hdf.object.Dataset;
import ncsa.hdf.object.Datatype;
import ncsa.hdf.object.FileFormat;
import ncsa.hdf.object.Group;
import ncsa.hdf.object.HObject;
import ncsa.hdf.object.ScalarDS;
import ncsa.hdf.object.h5.H5CompoundDS;
import ncsa.hdf.object.h5.H5Datatype;
import ncsa.hdf.object.h5.H5File;
import ncsa.hdf.object.h5.H5ScalarDS;
/**
* Test object at the ncsa.hdf.object package.
* <p>
*
* @version 1.3.0 9/21/2006
* @author Peter X. Cao
*
*/
public class TestH5Object
{
private final static String FILE_NAME = "TestH5Object.h5";
private final static String FILE_NAME2 = "../../TestH5Obejct2.h5";
private final static String NAME_GROUP = "/g0";
private final static String NAME_GROUP_ATTR = "/g0_attr";
private final static String NAME_GROUP_SUB = "/g0/g00";
private final static String NAME_DATASET_INT = "/dataset_int";
private final static String NAME_DATASET_FLOAT = "/dataset_float";
private final static String NAME_DATASET_CHAR = "/dataset_byte";
private final static String NAME_DATASET_STR = "/dataset_str";
private final static String NAME_DATASET_ENUM = "/dataset_enum";
private final static String NAME_DATASET_ATTR = "/dataset_with_attr";
private final static String NAME_DATASET_COMPOUND = "/comp_dataset";
private final static String NAME_DATASET_SUB = "/g0/dataset_int";
private final static String NAME_DATASET_SUB_SUB = "/g0/g00/dataset_float";
private final static H5File H5FILE = new H5File();
private final static long DIM1 = 50;
private final static long DIM2 = 10;
private final static long[] DIMs = {DIM1, DIM2};
private final static long[] CHUNKs = {DIM1/2, DIM2/2};
// private final static int RANK = 2;
private final static int STR_LEN = 20;
private final static int DIM_SIZE = (int)(DIM1*DIM2);;
/* testing data */
private final static int[] DATA_INT = new int[DIM_SIZE];
private final static float[] DATA_FLOAT = new float[DIM_SIZE];
private final static byte[] DATA_BYTE = new byte[DIM_SIZE];
private final static String[] DATA_STR = new String[DIM_SIZE];
private final static int[] DATA_ENUM = new int[DIM_SIZE];
private final static Vector<Object> DATA_COMP = new Vector<Object>(3);
private static PrintStream out = null;
/**
* Constructs an instance of TestH5Object.
* @param out_stream the out stream for printing the test results.
*/
public TestH5Object(final PrintStream print_stream)
{
if (print_stream == null) {
out = System.out;
}
else {
out = print_stream;
}
for (int i = 0; i < DIM_SIZE; i++) {
DATA_INT[i] = i;
DATA_FLOAT[i] = i + i/100.0f;
DATA_BYTE[i] = (byte)Math.IEEEremainder(i, 127);
DATA_STR[i] = "str" + i;
DATA_ENUM[i] = (int)Math.IEEEremainder(i, 2);
}
DATA_COMP.add(0, DATA_INT);
DATA_COMP.add(1, DATA_FLOAT);
DATA_COMP.add(2, DATA_STR);
}
private static final void passed(final String message) {
out.println("PASSED:\t"+message);
}
private static final void failed(final String message, final Exception err, final H5File file) {
out.println("FAILED***:\t"+message +"--"+err);
try { file.close(); } catch (final Exception ex) {}
}
/**
* Check if all the data values of two data buffers are the same
* @param buf1 the first buffer to compare
* @param buf2 the second buffer to compare
* @return true if all the values equal; otherwise, returns false.
*/
private static final boolean dataEquals(final Object buf1, final Object buf2) {
// array cannot be null
if ((buf1 == null) || (buf2==null)) {
return false;
}
// must be array
if (!buf1.getClass().isArray() || !buf2.getClass().isArray()) {
return false;
}
// must be the same kind of array
final String cname = buf1.getClass().getName();
if (!cname.equals(buf2.getClass().getName())) {
return false;
}
// must have the same size
final int n = Array.getLength(buf1);
if (n != Array.getLength(buf2)) {
return false;
}
if (cname.equals("[I")) {
final int[] data1 = (int[])buf1;
final int[] data2 = (int[])buf2;
for (int i = 0; i < n; i++) {
if (data1[i] != data2[i]) {
return false;
}
}
}
else if (cname.equals("[F")) {
final float[] data1 = (float[])buf1;
final float[] data2 = (float[])buf2;
for (int i = 0; i < n; i++) {
if (data1[i] != data2[i]) {
return false;
}
}
}
else if (cname.equals("[B")) {
final byte[] data1 = (byte[])buf1;
final byte[] data2 = (byte[])buf2;
for (int i = 0; i < n; i++) {
if (data1[i] != data2[i]) {
return false;
}
}
}
else if (cname.equals("[Ljava.lang.String;")) {
final String[] data1 = (String[])buf1;
final String[] data2 = (String[])buf2;
for (int i = 0; i < n; i++) {
if (!data1[i].equals(data2[i])) {
return false;
}
}
} else {
return false;
}
return true;
}
/**
* Create a test file.
*
* @param fname the name of the file to open
* @return true if successful; otherwise returns false
*/
private static final H5File create_test_file(final String fname, String message)
{
H5File file=null;
Group g0, g1, g00;
message += "\tCreate a test file: "+fname;
try {
file = (H5File)H5FILE.createInstance(fname, H5File.CREATE);
file.open();
} catch (final Exception ex) { failed(message, ex, file); return null; }
// create groups
try {
g0 = file.createGroup(NAME_GROUP, null);
g1 = file.createGroup(NAME_GROUP_ATTR, null);
g00 = file.createGroup(NAME_GROUP_SUB, null);
final long[] attrDims = {1};
final String attrName = "Test attribute";
final String[] attrValue = {"Test for group attribute"};
final Datatype attrType = new H5Datatype(Datatype.CLASS_STRING, attrValue[0].length()+1, -1, -1);
final Attribute attr = new Attribute(attrName, attrType, attrDims);
attr.setValue(attrValue);
g1.writeMetadata(attr);
} catch (final Exception ex) { failed(message, ex, file); return null; }
// create datasets
try {
file.createScalarDS(NAME_DATASET_INT, null, new H5Datatype(Datatype.CLASS_INTEGER, -1, -1, -1), DIMs, null, CHUNKs, 9, DATA_INT);
file.createScalarDS(NAME_DATASET_FLOAT, null, new H5Datatype(Datatype.CLASS_FLOAT, -1, -1, -1), DIMs, null, CHUNKs, 9, DATA_FLOAT);
file.createScalarDS(NAME_DATASET_CHAR, null, new H5Datatype(Datatype.CLASS_CHAR, -1, -1, -1), DIMs, null, CHUNKs, 9, DATA_BYTE);
file.createScalarDS(NAME_DATASET_STR, null, new H5Datatype(Datatype.CLASS_STRING, STR_LEN, -1, -1), DIMs, null, CHUNKs, 9, DATA_STR);
file.createScalarDS(NAME_DATASET_ENUM, null, new H5Datatype(Datatype.CLASS_ENUM, 4, -1, -1), DIMs, null, CHUNKs, 9, DATA_ENUM);
file.createScalarDS(NAME_DATASET_SUB, g0, new H5Datatype(Datatype.CLASS_INTEGER, -1, -1, -1), DIMs, null, CHUNKs, 9, DATA_INT);
file.createScalarDS(NAME_DATASET_SUB_SUB, g00, new H5Datatype(Datatype.CLASS_FLOAT, -1, -1, -1), DIMs, null, CHUNKs, 9, DATA_FLOAT);
file.createImage(NAME_DATASET_ATTR, null, new H5Datatype(Datatype.CLASS_INTEGER, 1, -1, -1), DIMs, null, CHUNKs, 9, 1, -1, DATA_BYTE);
final Datatype[] mdtypes = {new H5Datatype(Datatype.CLASS_INTEGER, -1, -1, -1), new H5Datatype(Datatype.CLASS_FLOAT, -1, -1, -1), new H5Datatype(Datatype.CLASS_STRING, STR_LEN, -1, -1)};
final String[] mnames = {"int", "float", "string"};
file.createCompoundDS(NAME_DATASET_COMPOUND, null, DIMs, null, CHUNKs, 9, mnames, mdtypes, null, DATA_COMP);
} catch (final Exception ex) { failed(message, ex, file); return null; }
try { file.close(); } catch (final Exception ex) {}
return file;
}
/**
* Test H5File create() function.
*
* @param fname the name of the file to create
* @return zero if successful; otherwise returns one
*/
private int test_H5File_create(final String fname)
{
H5File file = null;
String message = "";
message = "Create a new file -- new H5File()";
try {
file = new H5File(fname, H5File.CREATE);
file.open();
file.close();
} catch (final Exception ex) { failed(message, ex, file); return 1; }
passed(message);
message = "Create a new file -- H5File.create()";
try {
file = (H5File)H5FILE.createFile(fname, FileFormat.FILE_CREATE_DELETE);
file.open();
file.close();
} catch (final Exception ex) { failed(message, ex, file); return 1; }
passed(message);
message = "Create a new file -- H5File.createInstance()";
try {
file = (H5File)H5FILE.createInstance(fname, H5File.CREATE);
file.open();
file.close();
} catch (final Exception ex) { failed(message, ex, file); return 1; }
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File open() function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_open(final String fname)
{
H5File file = null;
String message = "";
message = "Testing open file with read/write access";
try {
file = new H5File(fname, H5File.CREATE);
file.open();
file.close();
} catch (final Exception ex) { failed(message, ex, file); return 1; }
message = "Open file with READ-ONLY access -- H5File.createInstance()";
try {
file = (H5File)H5FILE.createInstance(fname, H5File.READ);
file.close();
} catch (final Exception ex) { failed(message, ex, file); return 1; }
passed(message);
message = "Open file with WRITE access -- H5File.createInstance()";
try {
file = (H5File)H5FILE.createInstance(fname, H5File.WRITE);
file.close();
} catch (final Exception ex) { failed(message, ex, file); return 1; }
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File open() function with relative file path.
* The cwd may be changed at Dataset.read() by H5Dchdir_ext()
* to make it work for external datasets. We need to set it
* back before the file is closed/opened.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_open_relative_path(final String fname)
{
H5File file = null;
String message = "";
message = "H5File open() function with relative file path";
if ((create_test_file(fname, message)) == null) {
return 1;
}
try {
// test open/close file and open/close dataset
file = new H5File(fname, H5File.READ);
Dataset dset = (Dataset)file.get(NAME_DATASET_ATTR);
dset.getData();
file.close();
file = new H5File(fname, H5File.READ);
dset = (Dataset)file.get(NAME_DATASET_ATTR);
dset.getData();
file.close();
// test open file and open multiple datasets
file = new H5File(fname, H5File.READ);
dset = (Dataset)file.get(NAME_DATASET_ATTR);
dset.getData();
dset = (Dataset)file.get(NAME_DATASET_COMPOUND);
dset.getData();
dset = (Dataset)file.get(NAME_DATASET_SUB);
dset.getData();
file.close();
} catch (final Exception ex) { failed(message, ex, file); return 1; }
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File createGroup() function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
@SuppressWarnings("unused")
private int test_H5File_createGroup(final String fname)
{
H5File file = null;
String message = "";
file = new H5File(fname);
// create groups
Group g0 = null;
message = "Create a group at root -- H5Group.create()";
try {
g0 = file.createGroup("/g0", null);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
passed(message);
Group g00 = null;
message = "Create a group with absolute path -- H5Group.create()";
try {
g00 = file.createGroup("g0/g00", null);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
passed(message);
Group g01 = null;
message = "Create a group at non-root group -- H5Group.create()";
try {
g01 = file.createGroup("/g0/g01/", g0);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File createDatatype() function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
@SuppressWarnings("unused")
private int test_H5File_createDatatype(final String fname)
{
H5File file = null;
String message = "";
final int N = 5;
final int dtype_cls[] = {Datatype.CLASS_INTEGER, Datatype.CLASS_FLOAT,
Datatype.CLASS_CHAR, Datatype.CLASS_STRING, Datatype.CLASS_ENUM};
final String dtype_names[] = {"INTEGER", "FLOAT", "CHAR", "STRING", "ENUM"};
final String msgs[] = { "H5File.createDatatype(..., "+dtype_names[0]+")",
"H5File.createDatatype(..., "+dtype_names[1]+")",
"H5File.createDatatype(..., "+dtype_names[2]+")",
"H5File.createDatatype(..., "+dtype_names[3]+")",
"H5File.createDatatype(..., "+dtype_names[4]+")"};
message = "Test creating named datatypes";
try {
file = (H5File)H5FILE.createInstance(fname, H5File.CREATE);
file.open();
} catch (final Exception ex) { failed(message, ex, file); return 1; }
// create groups
Datatype dtype = null;
for (int i = 0; i < N; i++)
{
message = "Create a named datatype -- "+msgs[i];
try {
dtype = file.createDatatype(dtype_cls[i],Datatype.NATIVE, Datatype.NATIVE, Datatype.NATIVE, dtype_names[i]);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
passed(message);
}
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File createScalarDS function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_createScalarDS(final String fname)
{
H5File file = null;
String message = "";
Group pgroup = null;
final int N = 5;
final int dtype_cls[] = {Datatype.CLASS_INTEGER, Datatype.CLASS_FLOAT,
Datatype.CLASS_CHAR, Datatype.CLASS_STRING, Datatype.CLASS_ENUM};
final int dtype_sizes[] = {-1, -1, 1, 80, -1};
final String names[] = {"INTEGER", "FLOAT", "CHAR", "STRING", "ENUM"};
message = "Test creating ScalarDS";
try {
file = (H5File)H5FILE.createInstance(fname, H5File.CREATE);
file.open();
pgroup = (Group)file.get("/");
} catch (final Exception ex) { failed(message, ex, file); return 1; }
final Object[] all_data = new Object[N];
all_data[0] = DATA_INT;
all_data[1] = DATA_FLOAT;
all_data[2] = DATA_BYTE;
all_data[3] = DATA_STR;
all_data[4] = DATA_ENUM;
// create groups
Datatype dtype = null;
Dataset dset = null;
Object data_read = null;
for (int i = 0; i < N; i++) {
message = "Create/read/write a H5ScalarDS -- H5ScalarDS.create ()";
try {
dtype = new H5Datatype(dtype_cls[i], dtype_sizes[i], -1, -1);
dset = file.createScalarDS(names[i], pgroup, dtype, DIMs, null, CHUNKs, 9, all_data[i]);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
// test data values
try {
dset.init();
final long[] selectedDims = dset.getSelectedDims();
final long[] dims = dset.getDims();
final long[] starts = dset.getStartDims();
final int rank = dset.getRank();
// read all data
for (int j = 0; j < rank; j++) {
starts[j] = 0;
selectedDims[j] = dims[j];
}
data_read = dset.read();
} catch (final Exception ex) { failed(message, ex, file); return 1; }
if ( !dataEquals(all_data[i], data_read) ) {
failed(message, new HDF5LibraryException("Incorrect data values in file"), file);
return 1;
}
passed(message);
}
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File createLink function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_createLink(final String fname)
{
H5File file = null;
String message = "";
Group pgroup = null;
message = "Create a hard link -- H5File.createLink()";
try {
file = (H5File)H5FILE.createInstance(fname, H5File.CREATE);
file.open();
pgroup = (Group)file.get("/");
} catch (final Exception ex) { failed(message, ex, file); return 1; }
final String gname = "Group";
Group grp = null;
try {
grp = file.createGroup(gname, null);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
HObject hobj = null;
try {
hobj = file.createLink(pgroup, "link to "+gname, grp);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
final long oid[] = grp.getOID();
if (!hobj.equalsOID(oid)) {
failed(message, new HDF5LibraryException("link to the wrong object"), file);
return 1;
}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File createImage function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_createImage(final String fname)
{
H5File file = null;
String message = "";
Group pgroup = null;
message = "Ceate an image -- H5File.createImage()";
try {
file = (H5File)H5FILE.createInstance(fname, H5File.CREATE);
file.open();
pgroup = (Group)file.get("/");
} catch (final Exception ex) { failed(message, ex, file); return 1; }
// create groups
Datatype dtype = null;
Dataset dset = null;
Object data_read = null;
try {
dtype = new H5Datatype(Datatype.CLASS_INTEGER, 1, -1, -1);
dset = file.createImage("Image", pgroup, dtype, DIMs, null, CHUNKs, 9, 1, -1, DATA_BYTE);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
// test data value
try {
data_read = dset.read();
} catch (final Exception ex) { failed(message, ex, file); return 1; }
if (!dataEquals(DATA_BYTE, data_read) ) {
failed(message, new HDF5LibraryException("Incorrect data values in file"), file);
return 1;
}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File createCompoundDS function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_createCompoundDS(final String fname)
{
H5File file = null;
String message = "";
Group pgroup = null;
message = "Create/read/write a compound dataset -- H5CompoundDS.create()";
try {
file = (H5File)H5FILE.createInstance(fname, H5File.CREATE);
file.open();
pgroup = (Group)file.get("/");
} catch (final Exception ex) { failed(message, ex, file); return 1; }
final Vector<Object> data = new Vector<Object>();
data.add(0, DATA_INT);
data.add(1, DATA_FLOAT);
data.add(2, DATA_STR);
// create groups
final Datatype[] mdtypes = new H5Datatype[3];
final String[] mnames = {"int", "float", "string"};
Dataset dset = null;
try {
mdtypes[0] = new H5Datatype(Datatype.CLASS_INTEGER, -1, -1, -1);
mdtypes[1] = new H5Datatype(Datatype.CLASS_FLOAT, -1, -1, -1);
mdtypes[2] = new H5Datatype(Datatype.CLASS_STRING, STR_LEN, -1, -1);
dset = file.createCompoundDS("/CompoundDS", pgroup, DIMs, null, CHUNKs, 9, mnames, mdtypes, null, data);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
// test data values
List data_read = null;
try {
data_read = (List)dset.read();
} catch (final Exception ex) { failed(message, ex, file); return 1; }
if ( !dataEquals(DATA_INT, data_read.get(0)) ||
!dataEquals(DATA_FLOAT, data_read.get(1)) ||
!dataEquals(DATA_STR, data_read.get(2))) {
failed(message, new HDF5LibraryException("Incorrect data values in file"), file);
return 1;
}
// tests for subset
final H5CompoundDS compDS = (H5CompoundDS)dset;
int rank = compDS.getRank();
try {
if (rank <= 0) { compDS.init(); }
} catch (final Exception ex) {}
rank = compDS.getRank();
// read only one column but all rows
compDS.setMemberSelection(false); //unselect all members
compDS.selectMember(1); // select the second column
try {
data_read = (List)dset.read();
} catch (final Exception ex) { failed(message, ex, file); return 1; }
if (!dataEquals(DATA_FLOAT, data_read.get(0)) || (data_read.size() != 1)) {
failed(message, new HDF5LibraryException("incorrect data values from file"), file);
return 1;
}
// read only one row but all columns
compDS.setMemberSelection(true); //select all members, it is default
final int nmembers = compDS.getSelectedMemberCount();
final long[] count = compDS.getSelectedDims();
final long[] start = compDS.getStartDims();
for (int i = 0; i < rank; i++) {
start[i] = 2; // start the third data point
count[i] = 1; // select only one row (the third row)
}
try {
data_read = (List)dset.read();
} catch (final Exception ex) { failed(message, ex, file); return 1; }
if (nmembers != compDS.getMemberCount()) {
failed(message, new HDF5LibraryException("incorrect members selection"), file);
return 1;
}
for (int i = 0; i < nmembers; i++) {
if (Array.getLength(data_read.get(i)) != 1) {
failed(message, new HDF5LibraryException("incorrect row selection"), file);
return 1;
}
}
// create dataset at non root group
Group g0 = null;
try {
g0 = file.createGroup("/gg0", null);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
try {
mdtypes[0] = new H5Datatype(Datatype.CLASS_INTEGER, -1, -1, -1);
mdtypes[1] = new H5Datatype(Datatype.CLASS_FLOAT, -1, -1, -1);
mdtypes[2] = new H5Datatype(Datatype.CLASS_STRING, STR_LEN, -1, -1);
dset = file.createCompoundDS("/g0/CompoundDS/", g0, DIMs, null, CHUNKs, 9, mnames, mdtypes, null, data);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File copy function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_copy(final String fname)
{
H5File file = null;
String message = "";
Group pgroup = null;
message = "Copy dataset, group and attributes -- H5File.copy()";
try {
file = (H5File)H5FILE.createInstance(fname, H5File.CREATE);
file.open();
pgroup = (Group)file.get("/");
} catch (final Exception ex) { failed(message, ex, file); return 1; }
final int size = (int) (DIM1*DIM2);
final byte[] bdata = new byte[size];
for (int i = 0; i < size; i++) {
bdata[i] = (byte)Math.IEEEremainder(i, 127);
}
Group grp = null;
Datatype dtype = null;
Dataset dset = null;
try {
grp = file.createGroup("/Group", null);
dtype = new H5Datatype(Datatype.CLASS_INTEGER, 1, -1, -1);
dset = file.createImage("Dataset", pgroup, dtype, DIMs, null, CHUNKs, 9, 1, -1, bdata);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
try {
file.copy(dset, grp, null);
file.copy(grp, pgroup, "~Group");
} catch (final Exception ex) { failed(message, ex, file); return 1; }
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File getAttribute function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_getAttribute(final String fname)
{
H5File file = null;
String message = "";
message = "Read/write attributes from a group/dataset";
if ((create_test_file(fname, message)) == null) {
return 1;
}
try {
file = new H5File(fname);
final Dataset dset = (Dataset)file.get(NAME_DATASET_ATTR);
final int did = dset.open();
List<Attribute> attrs = H5File.getAttribute(did);
try { dset.close(did); } catch (final Exception ex2) {}
if ((attrs == null) || (attrs.size() < 1)) {
failed(message, new HDF5LibraryException("failed to read attributes from dataset"), file);
return 1;
}
attrs.clear();
final Group grp = (Group)file.get(NAME_GROUP_ATTR);
final int gid = grp.open();
attrs = H5File.getAttribute(gid);
try { grp.close(gid); } catch (final Exception ex2) {}
if ((attrs == null) || (attrs.size() < 1)) {
failed(message, new HDF5LibraryException("failed to read attributes from group"), file);
return 1;
}
} catch (final Exception ex) { failed(message, ex, file); return 1; }
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5File getHObject() function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5File_getHObject(final String fname)
{
String message = "";
message = "Get a group for a given path -- H5File.getHObject()";
if ((create_test_file(fname, message)) == null) {
return 1;
}
try {
HObject obj = FileFormat.getHObject(fname, NAME_GROUP);
if (obj == null) {
failed(message, new HDF5Exception("Failed to get a group"), null);
return 1;
}
try { obj.getFileFormat().close(); } catch (final Exception ex2) {}
obj = FileFormat.getHObject(fname+"://"+NAME_GROUP_SUB);
if (obj == null) {
failed(message, new HDF5Exception("Failed to get a group"), null);
return 1;
}
try { obj.getFileFormat().close(); } catch (final Exception ex2) {}
} catch (final Exception ex) { failed(message, ex, null); return 1; }
passed(message);
message = "Get a ScalarDS for a given path -- H5File.getHObject()";
try {
HObject obj = FileFormat.getHObject(fname, NAME_DATASET_INT);
if (obj == null) {
failed(message, new HDF5Exception("Failed to get a dataset"), null);
return 1;
}
try { obj.getFileFormat().close(); } catch (final Exception ex2) {}
obj = FileFormat.getHObject(fname+"://"+NAME_DATASET_FLOAT);
if (obj == null) {
failed(message, new HDF5Exception("Failed to get a dataset"), null);
return 1;
}
try { obj.getFileFormat().close(); } catch (final Exception ex2) {}
} catch (final Exception ex) { failed(message, ex, null); return 1; }
passed(message);
message = "Get a CompoundDS for a given path -- H5File.getHObject()";
try {
final HObject obj = FileFormat.getHObject(fname, NAME_DATASET_COMPOUND);
if (obj == null) {
failed(message, new HDF5Exception("Failed to get a compound dataset"), null);
return 1;
}
try { obj.getFileFormat().close(); } catch (final Exception ex2) {}
} catch (final Exception ex) { failed(message, ex, null); return 1; }
passed(message);
return 0;
}
/**
* Test HObject getFID() function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_HObject_getFID(final String fname)
{
H5File file = null;
String message = "";
int fid = 0;
Group pgroup = null;
message = "Get file identifier -- Group.getFID(), Dataset.getFID()";
try {
file = (H5File)H5FILE.createInstance(fname, H5File.CREATE);
fid = file.open();
pgroup = (Group)file.get("/");
} catch (final Exception ex) {failed(message, ex, file); return 1; }
if (fid != pgroup.getFID()) {
failed(message, new HDF5LibraryException("wrong object ID in group"), file);
return 1;
}
Datatype dtype = null;
Dataset dset = null;
try {
dtype = new H5Datatype(Datatype.CLASS_INTEGER, 1, -1, -1);
dset = file.createScalarDS("Dataset", pgroup, dtype, DIMs, null, CHUNKs, 9, null);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
if (fid != dset.getFID()) {
failed(message, new HDF5LibraryException("wrong object ID in dataset"), file);
return 1;
}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test HObject getName() function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_HObject_getName(final String fname)
{
H5File file = null;
String message = "";
int fid = 0;
Group pgroup = null;
message = "Get object name and path -- Group.getName(), Group.getPath()";
try {
file = (H5File)H5FILE.createInstance(fname, H5File.CREATE);
fid = file.open();
pgroup = (Group)file.get("/");
} catch (final Exception ex) { failed(message, ex, file); return 1; }
Group grp = null;
try {
grp = file.createGroup("/Group", null);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
Group grp2 = null;
try {
grp2 = file.createGroup("/Group/Group2", grp);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
if (!grp2.getName().endsWith("Group2")) {
failed(message, new HDF5LibraryException("wrong name for the group"), file);
return 1;
}
if (!grp2.getPath().endsWith("/Group/")) {
failed(message, new HDF5LibraryException("wrong path for the group"), file);
return 1;
}
if (!grp2.getFullName().endsWith("/Group/Group2")) {
failed(message, new HDF5LibraryException("wrong full path for the group"), file);
return 1;
}
Datatype dtype = null;
Dataset dset = null;
try {
dtype = new H5Datatype(Datatype.CLASS_INTEGER, 1, -1, -1);
dset = file.createScalarDS("Dataset", pgroup, dtype, DIMs, null, CHUNKs, 9, null);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
if (!dset.getName().endsWith("Dataset")) {
failed(message, new HDF5LibraryException("wrong name for the dataset"), file);
return 1;
}
if (!dset.getPath().endsWith("/")) {
failed(message, new HDF5LibraryException("wrong path for the dataset"), file);
return 1;
}
if (!dset.getFullName().endsWith("/Dataset")) {
failed(message, new HDF5LibraryException("wrong full path for the dataset"), file);
return 1;
}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test Group isRoot function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_Group_isRoot(final String fname)
{
H5File file = null;
String message = "";
Group pgroup = null;
message = "Check root group -- Group.isRoot()";
try {
file = (H5File)H5FILE.createInstance(fname, H5File.CREATE);
file.open();
pgroup = (Group)file.get("/");
} catch (final Exception ex) { failed(message, ex, file); return 1; }
if (!pgroup.isRoot()) {
failed(message, new HDF5LibraryException("failed to test root group"), file);
return 1;
}
Group grp = null;
try {
grp = file.createGroup("/Group", null);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
try { file.close(); } catch (final Exception ex) {}
try {
grp = (Group)file.get("/Group");
} catch (final Exception ex) { failed(message, ex, file); return 1; }
if (grp.isRoot()) {
failed(message, new HDF5LibraryException("failed to test non-root group"), file);
return 1;
}
try { grp.getFileFormat().close(); } catch (final Exception ex) {}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test Group getParent function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_Group_getParent(final String fname)
{
H5File file = null;
String message = "";
Group pgroup = null;
message = "Get parent group -- Group.getParent()";
try {
file = (H5File)H5FILE.createInstance(fname, H5File.CREATE);
file.open();
pgroup = (Group)file.get("/");
} catch (final Exception ex) { failed(message, ex, file); return 1; }
if(pgroup.getParent() != null) {
failed(message, new HDF5Exception("the parent of root group is not null"), file);
return 1;
}
// create groups
Group g0 = null;
try {
g0 = file.createGroup("/g0", pgroup);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
if(g0.getParent() == null) {
failed(message, new HDF5Exception("the parent of the group is null"), file);
return 1;
}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test Dataset byteToString function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_Dataset_byteToString(final String fname)
{
H5File file = null;
String message = "";
Group pgroup = null;
message = "Convert byte array to strings -- Dataset.byteToString()";
try {
file = (H5File)H5FILE.createInstance(fname, H5File.CREATE);
file.open();
pgroup = (Group)file.get("/");
} catch (final Exception ex) { failed(message, ex, file); return 1; }
final String[] sdata = new String[(int)DIM1];
for (int i = 0; i < DIM1; i++) {
sdata[i] = "str"+i;
}
// create groups
Datatype dtype = null;
Dataset dset = null;
String[] sdata_read = null;
byte[] bdata_read = null;
final long[] dims = {DIM1};
byte[] bdata = null;
try {
dtype = new H5Datatype(Datatype.CLASS_STRING, STR_LEN, -1, -1);
dset = file.createScalarDS("String", pgroup, dtype, dims, null, null, -1, sdata);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
// test data values
try {
dset.setConvertByteToString(false);
bdata = dset.readBytes();
bdata_read = (byte[])dset.read();
sdata_read = Dataset.byteToString(bdata_read, STR_LEN);
bdata_read = Dataset.stringToByte(sdata, STR_LEN);
} catch (final Exception ex) { failed(message, ex, file); return 1; }
if ( !dataEquals(bdata, bdata_read) ) {
failed(message, new HDF5LibraryException("Incorrect data from stringToByte()"), file);
return 1;
}
if ( !dataEquals(sdata, sdata_read) ) {
failed(message, new HDF5LibraryException("Incorrect data from byteToString()"), file);
return 1;
}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test H5Datatype toNative() function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
@SuppressWarnings("unused")
private int test_H5Datatype_toNative(final String fname)
{
H5File file = null;
String message = "";
Group pgroup = null;
Datatype dtype = null;
int tid=-1, tid2=-1;
try {
file = (H5File)H5FILE.createInstance(fname, H5File.CREATE);
file.open();
pgroup = (Group)file.get("/");
} catch (final Exception ex) { failed(message, ex, file); return 1; }
message = "Decode/encode datatypes -- H5Datatype.toNative()";
try {
dtype = file.createDatatype(Datatype.CLASS_INTEGER,-1, -1, -1);
tid = dtype.toNative();
if (!H5.H5Tequal(tid, HDF5Constants.H5T_NATIVE_INT)) {
failed(message, new HDF5Exception("Failed to convert native integer"), file);
return 1;
}
} catch (final Exception ex) { failed(message, ex, file); return 1; }
finally {
try { H5.H5Tclose(tid); } catch (final Exception ex) {}
}
try {
dtype = file.createDatatype(Datatype.CLASS_FLOAT,-1, -1, -1);
tid = dtype.toNative();
if (!H5.H5Tequal(tid, HDF5Constants.H5T_NATIVE_FLOAT)) {
failed(message, new HDF5Exception("Failed to convert native float"), file);
return 1;
}
} catch (final Exception ex) { failed(message, ex, file); return 1; }
finally {
try { H5.H5Tclose(tid); } catch (final Exception ex) {}
}
try {
dtype = file.createDatatype(Datatype.CLASS_CHAR, 1, -1, -1);
tid = dtype.toNative();
if (!H5.H5Tequal(tid, HDF5Constants.H5T_NATIVE_CHAR)) {
failed(message, new HDF5Exception("Failed to convert native char"), file);
return 1;
}
} catch (final Exception ex) { failed(message, ex, file); return 1; }
finally {
try { H5.H5Tclose(tid); } catch (final Exception ex) {}
}
try {
dtype = file.createDatatype(Datatype.CLASS_STRING, STR_LEN, -1, -1);
tid = dtype.toNative();
tid2 = H5.H5Tcopy(HDF5Constants.H5T_C_S1);
H5.H5Tset_size(tid2, STR_LEN);
//H5.H5Tset_strpad(tid2, HDF5Constants.H5T_STR_NULLPAD); /* default is not NULL padding */
if (!H5.H5Tequal(tid, tid2)) {
failed(message, new HDF5Exception("Failed to convert string"), file);
return 1;
}
} catch (final Exception ex) { failed(message, ex, file); return 1;}
finally {
try { H5.H5Tclose(tid2); } catch (final Exception ex) {}
try { H5.H5Tclose(tid); } catch (final Exception ex) {}
}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Gets compound dataset information.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5CompoundDS_init(final String fname)
{
H5File file = null;
String message = "";
message = "Get information from a compound dataset -- H5CompoundDS.init()";
if ((file = create_test_file(fname, message)) == null) {
return 1;
}
try {
final CompoundDS obj = (CompoundDS) file.get(NAME_DATASET_COMPOUND);
if (obj == null) {
failed(message, new HDF5Exception("Failed to get "+NAME_DATASET_COMPOUND), file);
return 1;
}
obj.init();
final int nmembers = obj.getMemberCount();
final String[] mnames = obj.getMemberNames();
if ((nmembers <= 0) || (mnames == null) || (mnames.length != nmembers)) {
failed(message, new HDF5Exception("Failed to get information from "+NAME_DATASET_COMPOUND), file);
return 1;
}
try { obj.getFileFormat().close(); } catch (final Exception ex2) {}
} catch (final Exception ex) { failed(message, ex, file); return 1; }
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Updates scalar dataset values.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5ScalarDS_write(final String fname)
{
H5File file = null;
String message = "";
message = "Updates scalar dataset values -- H5ScalarDS.write()";
if ((file = create_test_file(fname, message)) == null) {
return 1;
}
final int temp_value = 99999;
try {
final ScalarDS obj = (ScalarDS) file.get(NAME_DATASET_INT);
if (obj == null) {
failed(message, new HDF5Exception("Failed to get "+NAME_DATASET_INT), file);
return 1;
}
final int[] data_int1 = (int[])obj.getData();
if (data_int1 == null) {
failed(message, new HDF5Exception("Failed to read data from "+NAME_DATASET_INT), file);
try { obj.getFileFormat().close(); } catch (final Exception ex2) {}
return 1;
}
data_int1[0] = data_int1[1] = data_int1[2] = data_int1[3] = temp_value;
obj.write();
obj.clear();
final int[] data_int2 = (int[])obj.getData();
if (data_int2 == null) {
failed(message, new HDF5Exception("Failed to read data from "+NAME_DATASET_INT), file);
try { obj.getFileFormat().close(); } catch (final Exception ex2) {}
return 1;
}
if ( !dataEquals(data_int1, data_int2) ) {
failed(message, new HDF5LibraryException("Incorrect data values in file"), file);
try { obj.getFileFormat().close(); } catch (final Exception ex2) {}
return 1;
}
try { obj.getFileFormat().close(); } catch (final Exception ex2) {}
} catch (final Exception ex) { failed(message, ex, file); return 1; }
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Updates compound dataset values.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5CompoundDS_write(final String fname)
{
H5File file = null;
String message = "";
message = "Updates compound dataset values -- H5CompoundDS.write()";
if ((file = create_test_file(fname, message)) == null) {
return 1;
}
final int temp_value = 99999;
try {
final CompoundDS obj = (CompoundDS) file.get(NAME_DATASET_COMPOUND);
if (obj == null) {
failed(message, new HDF5Exception("Failed to get "+NAME_DATASET_COMPOUND), file);
return 1;
}
final Vector buf1 = (Vector)obj.getData();
if (buf1 == null) {
failed(message, new HDF5Exception("Failed to read data from "+NAME_DATASET_COMPOUND), file);
try { obj.getFileFormat().close(); } catch (final Exception ex2) {}
return 1;
}
final int[] data_int1 = (int[])buf1.get(0);
data_int1[0] = data_int1[1] = data_int1[2] = data_int1[3] = temp_value;
obj.write();
obj.clearData();
final Vector buf2 = (Vector)obj.getData();
if (buf2 == null) {
failed(message, new HDF5Exception("Failed to read data from "+NAME_DATASET_COMPOUND), file);
try { obj.getFileFormat().close(); } catch (final Exception ex2) {}
return 1;
}
final int[] data_int2 = (int[])buf2.get(0);
if ( !dataEquals(data_int1, data_int2) ) {
failed(message, new HDF5LibraryException("Incorrect data values in file"), file);
try { obj.getFileFormat().close(); } catch (final Exception ex2) {}
return 1;
}
try { obj.getFileFormat().close(); } catch (final Exception ex2) {}
} catch (final Exception ex) { failed(message, ex, file); return 1; }
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Updates compound dataset values row by row (bug#847).
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_H5CompoundDS_write_row_by_row(final String fname)
{
List list = null;
final int TEST_INT_VALUE = 999999999;
long[] count, start, dims;
int rank, nmembers, nrows = 1;
H5File file = null;
CompoundDS dset;
String message = "";
message = "Updates compound dataset values row by row -- H5CompoundDS.write()";
if ((create_test_file(fname, message)) == null) {
return 1;
}
try {
for (int rowIdx = 0; rowIdx < nrows; rowIdx++) {
// open the test file
file = new H5File(fname, H5File.WRITE);
file.open();
// retrieve the compound dataset
dset = (CompoundDS)file.get(NAME_DATASET_COMPOUND);
dset.init();
// get dataspace information
rank = dset.getRank();
count = dset.getSelectedDims();
start = dset.getStartDims();
dims = dset.getDims();
nmembers = dset.getMemberCount();
nrows = (int)dims[0];
// select one row only
for (int i = 0; i < rank; i++) {
count[i] = 1;
}
// select different rows
start[0] = rowIdx;
// 1) read the table cell (using dataset selection to select only that row of the table)
list = (List)dset.read();
// 2) re-initialize the Dataset
dset.init();
// 3) call 'Dataset.clearData()'
dset.clearData();
// 4) call 'Dataset.getData()'
list = (List)dset.read();
// 5) change the correct column/row **, col0/row0
final int[] read_row_data = (int []) list.get(0);
read_row_data[rowIdx] = TEST_INT_VALUE;
// 6) call 'Dataset.write()'
dset.write(list);
// 7) close the file
file.close();
// 8) reopen the file and read the table cell as in step 1
file.open();
// 9) assert that the value has been changed and is correct
dset = (CompoundDS)file.get(NAME_DATASET_COMPOUND);
dset.init();
rank = dset.getRank();
count = dset.getSelectedDims();
start = dset.getStartDims();
dims = dset.getDims();
nmembers = dset.getMemberCount();
for (int i = 0; i < rank; i++) {
start[i] = 0;
count[i] = 1;
}
list = (List)dset.read();
final int[] write_row_data = (int[]) list.get(0);
if (write_row_data[0] != TEST_INT_VALUE) {
failed(message, new HDF5LibraryException("Incorrect data values in file"), file);
return 1;
}
file.close();
}
} catch (final Exception ex) { failed(message, ex, file); return 1; }
passed(message);
return 0;
}
/**
* Test read/re-read String datasets.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
private int test_HDF5ScalarDS_str(final String fname)
{
H5File file = null;
final String message = "Test read/re-read String datasets";
if ((create_test_file(fname, message)) == null) {
return 1;
}
file = new H5File(fname);
try {
final H5ScalarDS dataset = (H5ScalarDS)file.get(NAME_DATASET_STR);
dataset.init();
final long[] start = dataset.getStartDims();
final long[] count = dataset.getSelectedDims();
start[0] = 0;
count[0] = 1;
dataset.getData();
dataset.init();
dataset.clearData();
dataset.getData();
} catch (final Exception ex ) { failed(message, ex, file); return 1; }
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
/**
* Test ***func name*** function.
*
* @param fname the name of the file to open
* @return zero if successful; otherwise returns one
*/
/*
private int test_temp(final String fname)
{
final H5File file = null;
final String message = "***********func name*********";
if ((file = create_test_file(fname, message)) == null) {
return 1;
}
try {
} catch (final Exception ex ) { failed(message, ex, file); return 1;}
passed(message);
try { file.close(); } catch (final Exception ex) {}
return 0;
}
*/
/*****************************************************************************
* Main routine for the testing. Use "-f" to save the test result to a log file.
* If "-f" flag is specified, the test results will printed to System.out.
* <p>
* For example, "test.object.TestH5Object -f test.log" to save the test results
* at file test.log.
* @param args
****************************************************************************/
public static void main(final String[] args)
{
PrintStream printStream = null;
int numOfFails = 0;
final int n = args.length;
if ((n > 1) && args[0].equals("-f")) {
try {
printStream = new PrintStream(new BufferedOutputStream(
new FileOutputStream(args[1])));
}
catch (final FileNotFoundException ex) {
printStream = null;
ex.printStackTrace();
}
}
final TestH5Object test = new TestH5Object(printStream);
numOfFails += test.test_H5File_create(FILE_NAME);
numOfFails += test.test_H5File_open(FILE_NAME);
numOfFails += test.test_H5File_open_relative_path(FILE_NAME2);
numOfFails += test.test_H5File_createGroup(FILE_NAME);
numOfFails += test.test_H5File_createDatatype(FILE_NAME);
numOfFails += test.test_H5File_createLink(FILE_NAME);
numOfFails += test.test_H5File_createImage(FILE_NAME);
numOfFails += test.test_H5File_createScalarDS(FILE_NAME);
numOfFails += test.test_H5File_createCompoundDS(FILE_NAME);
numOfFails += test.test_H5File_copy(FILE_NAME);
numOfFails += test.test_H5File_getAttribute(FILE_NAME);
numOfFails += test.test_H5File_getHObject(FILE_NAME);
numOfFails += test.test_HObject_getFID(FILE_NAME);
numOfFails += test.test_HObject_getName(FILE_NAME);
numOfFails += test.test_Group_isRoot(FILE_NAME);
numOfFails += test.test_Group_getParent(FILE_NAME);
numOfFails += test.test_Dataset_byteToString(FILE_NAME);
numOfFails += test.test_H5Datatype_toNative(FILE_NAME);
numOfFails += test.test_H5CompoundDS_init(FILE_NAME);
numOfFails += test.test_H5ScalarDS_write(FILE_NAME);
numOfFails += test.test_H5CompoundDS_write(FILE_NAME);
numOfFails += test.test_H5CompoundDS_write_row_by_row(FILE_NAME);
numOfFails += test.test_HDF5ScalarDS_str(FILE_NAME);
if (numOfFails <= 0) {
TestH5Object.out.println("\nAll tests passed.\n\n");
}
else if (numOfFails == 1) {
TestH5Object.out.println("\n*** 1 test failed.\n\n");
}
else {
TestH5Object.out.println("\n*** "+numOfFails+" tests failed.\n\n");
}
}
}
|