#!./common/bats/bin/bats 
# -*-sh-*-

source ./common/test_helper_functions.bash || exit 1
source ./common/functions || exit 1

PKG=HDF5
testname=libs/HDF5_1_8_API_h5_wrapper-tests

check_rms

@test "[$testname] Verify h5ex_g_create ($LMOD_FAMILY_COMPILER)" {
    binary=h5ex_g_create
    if [ ! -x "$binary" ];then
        flunk "$binary binary not available"
    fi

    run "./$binary"
    assert_success

    if [ ! -s "$binary.h5" ];then
	flunk "$binary.h5 file not created"
    else
	h5dump $binary.h5  > tmp.test
	run cmp -s tmp.test ./testfiles/$binary.ddl
	assert_success
    fi

    rm -f h5ex_g_create.h5
    
}


@test "[$testname] Verify h5ex_g_iterate ($LMOD_FAMILY_COMPILER)" {
    binary=h5ex_g_iterate
    if [ ! -x "$binary" ];then
        flunk "$binary binary not available"
    fi

    run "./$binary"
    assert_output "Objects in root group:
  Dataset: DS1
  Datatype: DT1
  Group: G1
  Dataset: L1"
}

@test "[$testname] Verify h5ex_g_traverse ($LMOD_FAMILY_COMPILER)" {
    binary=h5ex_g_traverse
    if [ ! -x "$binary" ];then
        flunk "$binary binary not available"
    fi

    run "./$binary"
    assert_output "/ {
  Group: group1 {
    Dataset: dset1
    Group: group3 {
      Dataset: dset2
      Group: group4 {
        Group: group1 {
          Group: group5 {
            Warning: Loop detected!
          }
        }
        Group: group2 {
        }
      }
    }
  }
  Group: group2 {
    Dataset: dset2
    Group: group4 {
      Group: group1 {
        Group: group5 {
          Dataset: dset1
          Group: group3 {
            Warning: Loop detected!
          }
        }
      }
      Group: group2 {
      }
    }
  }
}"
}

@test "[$testname] Verify h5ex_g_visit ($LMOD_FAMILY_COMPILER)" {
    binary=h5ex_g_visit
    if [ ! -x "$binary" ];then
        flunk "$binary binary not available"
    fi

    run "./$binary"
    assert_output "Objects in the file:
/  (Group)
/group1  (Group)
/group1/dset1  (Dataset)
/group1/group3  (Group)
/group1/group3/group4  (Group)
/group1/group3/group4/group1  (Group)
/group1/group3/group4/group2  (Group)

Links in the file:
/group1  (Group)
/group1/dset1  (Dataset)
/group1/group3  (Group)
/group1/group3/dset2  (Dataset)
/group1/group3/group4  (Group)
/group1/group3/group4/group1  (Group)
/group1/group3/group4/group1/group5  (Group)
/group1/group3/group4/group2  (Group)
/group2  (Group)"

}

@test "[$testname] Verify h5ex_g_compact ($LMOD_FAMILY_COMPILER)" {
    binary=h5ex_g_compact
    if [ ! -x "$binary" ];then
        flunk "$binary binary not available"
    fi

    run "./$binary"
    assert_success

    if [ ! -s "${binary}1.h5" ];then
	flunk "${binary}1.h5 file not created"
    else
	h5dump ${binary}1.h5  > tmp.test
	run cmp -s tmp.test ./testfiles/${binary}1.ddl
	assert_success
    fi

    if [ ! -s "${binary}2.h5" ];then
	flunk "${binary}2.h5 file not created"
    else
	h5dump ${binary}2.h5  > tmp.test
	run cmp -s tmp.test ./testfiles/${binary}2.ddl
	assert_success
    fi

    rm -f h5ex_g_compact*.h5
    
}

@test "[$testname] Verify h5ex_g_phase ($LMOD_FAMILY_COMPILER)" {
    binary=h5ex_g_phase
    if [ ! -x "$binary" ];then
        flunk "$binary binary not available"
    fi

    run "./$binary"
    assert_output "1 Group : Storage type is H5G_STORAGE_TYPE_COMPACT
2 Groups: Storage type is H5G_STORAGE_TYPE_COMPACT
3 Groups: Storage type is H5G_STORAGE_TYPE_COMPACT
4 Groups: Storage type is H5G_STORAGE_TYPE_COMPACT
5 Groups: Storage type is H5G_STORAGE_TYPE_COMPACT
6 Groups: Storage type is H5G_STORAGE_TYPE_DENSE
7 Groups: Storage type is H5G_STORAGE_TYPE_DENSE

6 Groups: Storage type is H5G_STORAGE_TYPE_DENSE
5 Groups: Storage type is H5G_STORAGE_TYPE_DENSE
4 Groups: Storage type is H5G_STORAGE_TYPE_DENSE
3 Groups: Storage type is H5G_STORAGE_TYPE_DENSE
2 Groups: Storage type is H5G_STORAGE_TYPE_COMPACT
1 Group : Storage type is H5G_STORAGE_TYPE_COMPACT
0 Groups: Storage type is H5G_STORAGE_TYPE_COMPACT"
}

@test "[$testname] Verify h5ex_g_corder ($LMOD_FAMILY_COMPILER)" {
    binary=h5ex_g_corder
    if [ ! -x "$binary" ];then
        flunk "$binary binary not available"
    fi

    run "./$binary"
    assert_output "Traversing group using alphabetical indices:

Index 0: 5
Index 1: D
Index 2: F
Index 3: H

Traversing group using creation order indices:

Index 0: H
Index 1: D
Index 2: F
Index 3: 5"

}

@test "[$testname] Verify h5ex_g_intermediate ($LMOD_FAMILY_COMPILER)" {
    binary=h5ex_g_intermediate
    if [ ! -x "$binary" ];then
        flunk "$binary binary not available"
    fi

    run "./$binary"
    assert_output "Objects in the file:
/  (Group)
/G1  (Group)
/G1/G2  (Group)
/G1/G2/G3  (Group)"

}

