~ubuntu-branches/ubuntu/wily/libsereal-encoder-perl/wily

« back to all changes in this revision

Viewing changes to author_tools/stringify_test.c

  • Committer: Package Import Robot
  • Author(s): Alexandre Mestiashvili
  • Date: 2013-02-20 08:29:14 UTC
  • Revision ID: package-import@ubuntu.com-20130220082914-dljb6eixvtj2m1v2
Tags: upstream-0.31
ImportĀ upstreamĀ versionĀ 0.31

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <sys/types.h>
 
4
#include <math.h>
 
5
 
 
6
#define DEBUGHACK 0
 
7
 
 
8
/* Call with a string buffer of at least ref_len+1 chars. */
 
9
static char *
 
10
my_itoa_check(register unsigned int i, char out_str[], char *ref_str, size_t ref_len)
 
11
{
 
12
    register char *out_ptr = out_str + ref_len;
 
13
    register char *ref_ptr = ref_str + ref_len-1;
 
14
 
 
15
    *out_ptr-- = 0;
 
16
 
 
17
    while(1) {
 
18
        *out_ptr = i % 10 + 0x30;
 
19
        if ( !(i /= 10) )
 
20
            break;
 
21
        if (DEBUGHACK) printf("!%c! !%c!\n", *out_ptr, *ref_ptr);
 
22
        if ( *ref_ptr-- != *out_ptr-- ) {
 
23
            if (DEBUGHACK) printf("NOT SAME: '%c' '%c'\n", out_ptr[1], ref_ptr[1]);
 
24
            return NULL;
 
25
        }
 
26
    }
 
27
 
 
28
    if (DEBUGHACK) printf("!!! ref_str='%p' ref_ptr='%p' out_str='%p' out_ptr='%p\n'", ref_str, ref_ptr, out_str, out_ptr);
 
29
    if ( (*ref_ptr != *out_ptr) || (ref_str != ref_ptr) )
 
30
    {
 
31
        if (DEBUGHACK) printf("NOT SAME: '%c' '%c'\n", out_ptr[1], ref_ptr[1]);
 
32
        return NULL;
 
33
    }
 
34
 
 
35
    return out_ptr;
 
36
}
 
37
 
 
38
static int
 
39
my_integer_string_equality_check(register unsigned int i, char *ref_str, size_t ref_len)
 
40
{
 
41
    register char *ref_ptr = ref_str + ref_len-1;
 
42
    char test_char;
 
43
 
 
44
    while(1) {
 
45
        test_char = i % 10 + 0x30;
 
46
        if ( !(i /= 10) )
 
47
            break;
 
48
        if (DEBUGHACK) printf("!%c! !%c!\n", test_char, *ref_ptr);
 
49
        if ( *ref_ptr-- != test_char ) {
 
50
            if (DEBUGHACK) printf("NOT SAME: '%c' '%c'\n", test_char, ref_ptr[1]);
 
51
            return 0;
 
52
        }
 
53
    }
 
54
 
 
55
    if (DEBUGHACK) printf("!!! ref_str='%p' ref_ptr='%p' test_char='%c' ref-char='%c'\n", ref_str, ref_ptr, test_char, *ref_ptr);
 
56
    if ( (*ref_ptr != test_char) || (ref_str != ref_ptr) )
 
57
    {
 
58
        if (DEBUGHACK) printf("NOT SAME: '%c' '%c'\n", test_char, ref_ptr[1]);
 
59
        return 0;
 
60
    }
 
61
 
 
62
    return 1;
 
63
}
 
64
 
 
65
int
 
66
main(int argc, char** argv) {
 
67
    unsigned int i = 1234;
 
68
    unsigned int in_len = (argc >= 2 ? atoi(argv[1]) : 3);
 
69
    char *o;
 
70
    char outstr[64];
 
71
    unsigned int ord;
 
72
    unsigned int j;
 
73
    unsigned int ref_num;
 
74
    char *str = malloc(in_len+1);
 
75
    ord = 1;
 
76
    for (j = 0; j < in_len; ++j) {
 
77
        str[j] = 0x30 + ((j+1) % 10);
 
78
        ord *= 10;
 
79
    }
 
80
    str[in_len] = '\0';
 
81
    ref_num = atoi(str);
 
82
 
 
83
    /*
 
84
     * o = my_itoa_check(i, outstr, str, in_len);
 
85
     * printf("SAME IF NOT NULL: '%s'\n", o);
 
86
     */
 
87
 
 
88
    for (j = 0; j < 100000000; ++j) {
 
89
        int k;
 
90
        i = (!(i % 2) ? ref_num : j % ord);
 
91
        if (DEBUGHACK) printf("%i %i %i %i '%s'\n", j, i, ord, in_len, str);
 
92
        k = my_integer_string_equality_check(i, str, in_len);
 
93
        if (k == 1) {
 
94
            if (DEBUGHACK) printf("!EUREKA!\n");
 
95
        }
 
96
    }
 
97
 
 
98
    /*
 
99
    if (my_integer_string_equality_check(i, str, 3)) {
 
100
        printf("YAY, SAME!\n");
 
101
    } else {
 
102
        printf("NOOOOO, NOT SAME!\n");
 
103
    }
 
104
    */
 
105
 
 
106
    return 1;
 
107
}