Skip to content

Commit

Permalink
Add some C-API test coverage, and add request_values
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisspyB committed Dec 11, 2024
1 parent d066c18 commit 1b03851
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/metkit/api/metkit_c.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,21 @@ int metkit_request_value(const metkit_request_t* request, const char* param, int
});
}

int metkit_request_values(const metkit_request_t* request, const char* param, const char** values[], size_t* numValues) {
return tryCatch([request, param, values, numValues] {
ASSERT(request);
ASSERT(param);
ASSERT(values);
ASSERT(numValues);
const std::vector<std::string>& v = request->values(param);
*numValues = v.size();
*values = new const char*[v.size()];
for (size_t i = 0; i < v.size(); i++) {
(*values)[i] = v[i].c_str();
}
});
}

int metkit_request_expand(const metkit_request_t* request, metkit_request_t* expandedRequest, bool inherit, bool strict) {
return tryCatch([request, expandedRequest, inherit, strict] {
ASSERT(request);
Expand Down
9 changes: 9 additions & 0 deletions src/metkit/api/metkit_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ int metkit_request_count_values(const metkit_request_t* request, const char* par
*/
int metkit_request_value(const metkit_request_t* request, const char* param, int index, const char** value);

/** Returns values for specific parameter Request object
* @param request Request instance
* @param param parameter name in request
* @param values list of values for param in request
* @param numValues number of values for param in request
* @return int Error code
*/
int metkit_request_values(const metkit_request_t* request, const char* param, const char** values[], size_t* numValues);

/** Populates empty Request object by expanding existing request
* @param request Request instance to be expanded
* @param expandedRequest empty Request instance to be populated
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ ecbuild_add_test( TARGET metkit_test_gribhandle
NO_AS_NEEDED
ENVIRONMENT "${metkit_env}")

list(APPEND testFileSuffixes typesfactory expand request param_axis steprange_axis time hypercube type_levelist )
list(APPEND testFileSuffixes typesfactory expand request param_axis steprange_axis time hypercube type_levelist c_api )

foreach(test IN LISTS testFileSuffixes)
ecbuild_add_test( TARGET "metkit_test_${test}"
Expand Down
81 changes: 81 additions & 0 deletions tests/test_c_api.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* (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.
*/

/// @file test_c_api.cc
/// @date Dec 2024
/// @author Christopher Bradley

#include "metkit/api/metkit_c.h"
#include "eckit/testing/Test.h"

using namespace eckit::testing;
namespace metkit::test {

CASE( "Request get/set" ) {

metkit_request_t* request = nullptr;

metkit_new_request(&request);
EXPECT(request);

// set/get verb
metkit_request_set_verb(request, "retrieve");
const char* verb = nullptr;
metkit_request_verb(request, &verb);
EXPECT(strcmp(verb, "retrieve") == 0);

// set array of values
const char* dates[] = {"20200101", "20200102", "20200103"};
metkit_request_add(request, "date", dates, 3);

// set single value
const char* expver = "xxxx";
metkit_request_add(request, "expver", &expver, 1);

// check values
bool has = false;
metkit_request_has_param(request, "date", &has);
EXPECT(has);

metkit_request_has_param(request, "random", &has);
EXPECT(!has);

size_t count = 0;
metkit_request_count_values(request, "date", &count);
EXPECT_EQUAL(count, 3);

for (size_t i = 0; i < count; i++) {
const char* value = nullptr;
metkit_request_value(request, "date", i, &value);
EXPECT(strcmp(value, dates[i]) == 0);
}

// all values
const char** values = nullptr;
count = 0;
metkit_request_values(request, "date", &values, &count);
EXPECT_EQUAL(count, 3);

for (size_t i = 0; i < count; i++) {
EXPECT(strcmp(values[i], dates[i]) == 0);
}

// done
metkit_free_request(request);
}

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

} // namespace metkit::test

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

0 comments on commit 1b03851

Please sign in to comment.