YAC 3.13.0
Yet Another Coupler
Loading...
Searching...
No Matches
test_yac_mpi.c
Go to the documentation of this file.
1// Copyright (c) 2025 The YAC Authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <mpi.h>
9#include "tests.h"
10#include "yac_mpi_internal.h"
11
18int main(void) {
19
20 MPI_Init(NULL, NULL);
21
22 int comm_rank;
23 MPI_Comm_rank(MPI_COMM_WORLD, &comm_rank);
24
25 // Test 1: Normal string (incl. empty one)
26 {
27 char const * test_strings[] = {
28 "Hello, YAC!",
29 "",
30 "This is a longer test string to check packing and unpacking "
31 "functionality."
32 };
33 enum {
34 NUM_TEST_STRINGS = sizeof(test_strings) / sizeof(test_strings[0])};
35
36 for (size_t str_idx = 0; str_idx < NUM_TEST_STRINGS; ++str_idx) {
37 for (int allow_null = 0; allow_null <= 1; ++allow_null) {
38
39 const char *test_str = test_strings[str_idx];
40 size_t pack_size =
41 yac_string_get_pack_size("test", test_str, MPI_COMM_WORLD, allow_null);
42 void *buffer = malloc(pack_size);
43 int pos = 0;
45 "test", test_str, buffer, (int)pack_size, &pos, MPI_COMM_WORLD,
46 allow_null);
47
48 pos = 0;
49 char *unpacked =
50 yac_string_unpack(buffer, (int)pack_size, &pos, MPI_COMM_WORLD);
51
52 if (!unpacked || strcmp(unpacked, test_str) != 0) {
53 PUT_ERR("Normal string pack/unpack");
54 }
55 free(unpacked);
56 free(buffer);
57 } // for allow_null
58 } // for str_idx
59 }
60
61 // Test 2: NULL string with allow_null = 1
62 {
63 int allow_null = 1;
64 size_t pack_size =
65 yac_string_get_pack_size("test", NULL, MPI_COMM_WORLD, allow_null);
66 void *buffer = malloc(pack_size);
67 int pos = 0;
69 "test", NULL, buffer, (int)pack_size, &pos, MPI_COMM_WORLD, allow_null);
70
71 pos = 0;
72 char *unpacked =
73 yac_string_unpack(buffer, (int)pack_size, &pos, MPI_COMM_WORLD);
74 if (unpacked != NULL) {
75 PUT_ERR("FAILED: NULL string pack/unpack");
76 }
77 free(buffer);
78 }
79
80 MPI_Finalize();
81 return TEST_EXIT_CODE;
82}
#define TEST_EXIT_CODE
Definition tests.h:14
#define PUT_ERR(string)
Definition tests.h:10
double * buffer
size_t yac_string_get_pack_size(char const *caller, char const *string, MPI_Comm comm, int allow_null)
Compute number of bytes required to pack a string for MPI transport.
Definition yac_mpi.c:646
char * yac_string_unpack(void const *buffer, int buffer_size, int *position, MPI_Comm comm)
Unpack a C string from a buffer packed with yac_string_pack.
Definition yac_mpi.c:697
void yac_string_pack(char const *caller, char const *string, void *buffer, int buffer_size, int *position, MPI_Comm comm, int allow_null)
Pack a C string into a provided buffer using MPI_Pack semantics.
Definition yac_mpi.c:667