File: test.c

package info (click to toggle)
cfengine3 3.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 20,256 kB
  • ctags: 9,613
  • sloc: ansic: 116,129; sh: 12,366; yacc: 1,088; makefile: 1,006; lex: 391; perl: 197; xml: 21; sed: 4
file content (55 lines) | stat: -rw-r--r-- 1,120 bytes parent folder | download
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
#include <test.h>

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <alloc.h>

char *file_read_string(FILE *in)
{
    fpos_t pos;
    long size;
    char *buffer;

    assert_int_equal(fgetpos(in, &pos), 0);

    assert_int_equal(fseek(in, 0, SEEK_END), 0);
    size = ftell(in);
    assert_true(size >= 0);

    assert_int_equal(fseek(in, 0, SEEK_SET), 0);

    buffer = xcalloc(size + 1L, sizeof(char));
    assert_int_equal(fread(buffer, 1, size, in), size);

    assert_int_equal(fsetpos(in, &pos), 0);

    return buffer;
}

void assert_file_equal(FILE *a, FILE *b)
{
    char *a_buffer = file_read_string(a);
    char *b_buffer = file_read_string(b);

    if (strcmp(a_buffer, b_buffer) != 0)
    {
        printf("\n=====\n%s != \n=====\n%s\n", a_buffer, b_buffer);
        fail();
    }

    free(a_buffer);
    free(b_buffer);
}

#define SMALL_DIFF 1e-14

void _assert_double_close(double left, double right, const char *const file, const int line)
{
    if (fabs(left - right) > SMALL_DIFF)
    {
        print_error("%f != %f (+- 1e-14)\n", left, right);
        _fail(file, line);
    }
}