1 #include <cstdio>
2 #include <cstring>
3 
4 #include "HighsIO.h"
5 #include "catch.hpp"
6 
7 const bool dev_run = false;
8 
9 char printedmsg[100000];
10 void* receiveddata = NULL;
11 
12 // callback that saves message away for comparison
myprintmsgcb(int level,const char * msg,void * msgcb_data)13 static void myprintmsgcb(int level, const char* msg, void* msgcb_data) {
14   strcpy(printedmsg, msg);
15   receiveddata = msgcb_data;
16 }
17 
mylogmsgcb(HighsMessageType type,const char * msg,void * msgcb_data)18 static void mylogmsgcb(HighsMessageType type, const char* msg,
19                        void* msgcb_data) {
20   strcpy(printedmsg, msg);
21   receiveddata = msgcb_data;
22 }
23 
24 TEST_CASE("msgcb", "[highs_io]") {
25   int dummydata = 42;
26 
27   HighsSetMessageCallback(myprintmsgcb, mylogmsgcb, (void*)&dummydata);
28 
29   int message_level = ML_MINIMAL;
30   HighsPrintMessage(stdout, message_level, 4, "Hi %s!", "HiGHS");
31   REQUIRE(strcmp(printedmsg, "Hi HiGHS!") == 0);
32   REQUIRE(receiveddata == &dummydata);
33 
34   /* printed at level 4 when level is 3 should not print */
35   *printedmsg = '\0';
36   message_level = 3;
37   HighsPrintMessage(stdout, message_level, 4, "Hi %s!", "HiGHS");
38   REQUIRE(*printedmsg == '\0');
39 
40   {
41     char longmsg[sizeof(printedmsg)];
42     memset(longmsg, 'H', sizeof(longmsg));
43     longmsg[sizeof(longmsg) - 1] = '\0';
44     HighsPrintMessage(stdout, message_level, 2, longmsg);
45     REQUIRE(strncmp(printedmsg, "HHHH", 4) == 0);
46     REQUIRE(strlen(printedmsg) <= sizeof(printedmsg));
47   }
48 
49   HighsLogMessage(stdout, HighsMessageType::INFO, "Hello %s!", "HiGHS");
50   REQUIRE(strlen(printedmsg) > 8);
51   REQUIRE(strcmp(printedmsg + 8, " [INFO   ] Hello HiGHS!\n") ==
52           0);  // begin of printedmsg is a timestamp, which we skip over
53   REQUIRE(receiveddata == &dummydata);
54 
55   {
56     char longmsg[sizeof(printedmsg)];
57     memset(longmsg, 'H', sizeof(longmsg));
58     longmsg[sizeof(longmsg) - 1] = '\0';
59     HighsLogMessage(stdout, HighsMessageType::WARNING, longmsg);
60     REQUIRE(strstr(printedmsg, "HHHH") != NULL);
61     REQUIRE(strlen(printedmsg) <= sizeof(printedmsg));
62   }
63 }
64