aboutsummaryrefslogtreecommitdiff
path: root/drive-sdk/test/anki_greatest.h
blob: 2d39a2bd3d22ef1317e68ddaf07cbcf3f94442a7 (plain)
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
/*
 * Copyright (c) 2014 Anki, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef INCLUDE_anki_greatest_h__
#define INCLUDE_anki_greatest_h__

#define ANKI_GREATEST_HEX_DUMP(buffer, len) \
    do {                                                \
        const uint8_t *b = (const uint8_t *)buffer;     \
        int i;                                          \
        for (i = 0; i < (len); ++i) {                   \
            fprintf(GREATEST_STDOUT, "%02x ", b[i]);    \
        }                                               \
    } while(0)

#define ANKI_GREATEST_ASSERT_BYTES_EQm(MSG, EXP, GOT, LEN)                          \
    do {                                                                \
        const void *exp_s = (const void *)(EXP);                                      \
        const void *got_s = (const void *)(GOT);                                      \
        const size_t len_s = (const size_t)(LEN);                                      \
        greatest_info.msg = MSG;                                        \
        greatest_info.fail_file = __FILE__;                             \
        greatest_info.fail_line = __LINE__;                             \
        if (0 != memcmp(exp_s, got_s, len_s)) {                         \
            fprintf(GREATEST_STDOUT, "Expected:\n####\n");              \
            ANKI_GREATEST_HEX_DUMP(exp_s, len_s);                       \
            fprintf(GREATEST_STDOUT, "\n####\n");                       \
            fprintf(GREATEST_STDOUT, "Got:\n####\n");                   \
            ANKI_GREATEST_HEX_DUMP(got_s, len_s);                       \
            fprintf(GREATEST_STDOUT, "\n####\n");                       \
            return -1;                                                  \
        }                                                               \
        greatest_info.msg = NULL;                                       \
    } while (0)

#define ANKI_GREATEST_ASSERT_BYTES_EQ(EXP, GOT, LEN) ANKI_GREATEST_ASSERT_BYTES_EQm(#EXP " != " #GOT, EXP, GOT, LEN)

#define ASSERT_BYTES_EQm(MSG, EXP, GOT, LEN) ANKI_GREATEST_ASSERT_BYTES_EQm(MSG, EXP, GOT, LEN)
#define ASSERT_BYTES_EQ(EXP, GOT, LEN)  ANKI_GREATEST_ASSERT_BYTES_EQ(EXP, GOT, LEN)

#endif