File: test_gribhandle.cc

package info (click to toggle)
metkit 1.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,932 kB
  • sloc: cpp: 14,098; sh: 2,532; python: 1,699; ansic: 71; makefile: 18
file content (65 lines) | stat: -rw-r--r-- 1,851 bytes parent folder | download | duplicates (3)
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
/*
 * (C) Copyright 1996- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation
 * nor does it submit to any jurisdiction.
 */

/// @date   Apr 2024
/// @author Christopher Bradley

#include <string>
#include <vector>

#include "eckit/filesystem/PathName.h"
#include "eckit/io/FileHandle.h"
#include "eckit/testing/Test.h"

#include "eccodes.h"

#include "metkit/codes/GribAccessor.h"
#include "metkit/codes/GribHandle.h"

namespace metkit::grib::test {

//-----------------------------------------------------------------------------

// Test that a gribhandle will point to the correct message in a file, given an offset.
CASE("File with two messages") {

    // The test file has two messages of different packing types, with some junk data in between.
    eckit::PathName path("synthetic_2msgs.grib");

    off_t* offsets;
    grib_context* c = nullptr;
    int n           = 0;
    int err         = codes_extract_offsets_malloc(c, path.asString().c_str(), PRODUCT_GRIB, &offsets, &n, 1);
    EXPECT(!err);
    EXPECT(n == 2);

    GribAccessor<std::string> packingType("packingType");
    std::vector<std::string> expected = {"grid_simple", "grid_ccsds"};

    eckit::FileHandle dh(path);
    dh.openForRead();

    for (int i = 0; i < n; i++) {
        GribHandle h(dh, offsets[i]);
        EXPECT(packingType(h) == expected[i]);
    }

    free(offsets);
    dh.close();
}

//-----------------------------------------------------------------------------

}  // namespace metkit::grib::test


int main(int argc, char** argv) {
    return eckit::testing::run_tests(argc, argv);
}