~ubuntu-branches/ubuntu/raring/maradns/raring

« back to all changes in this revision

Viewing changes to deadwood-2.4.10/src/Test.c

  • Committer: Bazaar Package Importer
  • Author(s): Kai Hendry
  • Date: 2010-01-24 12:17:40 UTC
  • mfrom: (1.1.13 upstream) (10.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100124121740-a4e1fjobwaouz443
Tags: 1.4.02-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2007 Sam Trenholme
 
2
 *
 
3
 * TERMS
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions
 
7
 * are met:
 
8
 *
 
9
 * 1. Redistributions of source code must retain the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer.
 
11
 * 2. Redistributions in binary form must reproduce the above copyright
 
12
 *    notice, this list of conditions and the following disclaimer in the
 
13
 *    documentation and/or other materials provided with the distribution.
 
14
 *
 
15
 * This software is provided 'as is' with no guarantees of correctness or
 
16
 * fitness for purpose.
 
17
 */
 
18
 
 
19
#include "DwStr.h"
 
20
#include "DwStr_functions.h"
 
21
#include <stdio.h>
 
22
 
 
23
/* This actually isn't a public function; we just have it declared here
 
24
 * so we can compile w/o warnings when -Wall is used */
 
25
uint32_t dwh_hash_compress(dw_str *obj);
 
26
 
 
27
int main() {
 
28
        dw_str *test = 0, *c1 = 0, *c2 = 0, *c3 = 0;
 
29
        int a = 0;
 
30
        test = dw_create(256);
 
31
        if(test == 0) {
 
32
                printf("String creation failed.\n");
 
33
                goto catch_main;
 
34
        }
 
35
        /* Add some strings to the function */
 
36
        if(dw_qspush((uint8_t *)"Life",test) == -1) {
 
37
                printf("Appending failed.\n");
 
38
                goto catch_main;
 
39
        }
 
40
        dw_stdout(test);
 
41
 
 
42
        if(dw_qspush((uint8_t *)"liberty",test) == -1) {
 
43
                printf("Appending failed.\n");
 
44
                goto catch_main;
 
45
        }
 
46
        dw_stdout(test);
 
47
 
 
48
        if(dw_qspush((uint8_t *)"happiness",test) == -1) {
 
49
                printf("Appending failed.\n");
 
50
                goto catch_main;
 
51
        }
 
52
        dw_stdout(test);
 
53
 
 
54
        /* Test dw_fetch_u16 */
 
55
        printf("16-bit value at offsets 0 2 4 7: ");
 
56
        printf("%d ",(unsigned int)dw_fetch_u16(test,0));
 
57
        printf("%d ",(unsigned int)dw_fetch_u16(test,2));
 
58
        printf("%d ",(unsigned int)dw_fetch_u16(test,4));
 
59
        printf("%d\n",(unsigned int)dw_fetch_u16(test,7));
 
60
 
 
61
        /* Test dw_copy and dw_substr */
 
62
        printf("TEST: dw_copy and dw_substr\n");
 
63
        c1 = dw_copy(test);
 
64
        c2 = dw_substr(test,-9,-1,0);
 
65
        c3 = dw_substr(test,5,7,0);
 
66
        dw_stdout(c1);
 
67
        dw_stdout(c2);
 
68
        dw_stdout(c3);
 
69
 
 
70
        printf("\n");
 
71
        printf("c1 hash: %x\n",(unsigned int)dwh_hash_compress(c1));
 
72
        printf("c2 hash: %x\n",(unsigned int)dwh_hash_compress(c2));
 
73
        printf("c3 hash: %x\n",(unsigned int)dwh_hash_compress(c3));
 
74
        printf("\n");
 
75
 
 
76
        /* Test dw_qspop */
 
77
        printf("TEST: dw_qspop\n");
 
78
        dw_destroy(c1);
 
79
        c1 = 0;
 
80
        c1 = dw_qspop(test);
 
81
        dw_stdout(c1);
 
82
        dw_destroy(c1);
 
83
        c1 = 0;
 
84
        c1 = dw_qspop(test);
 
85
        dw_stdout(c1);
 
86
        dw_destroy(c1);
 
87
        c1 = 0;
 
88
        c1 = dw_qspop(test);
 
89
        dw_stdout(c1);
 
90
        dw_destroy(c1);
 
91
        c1 = 0;
 
92
 
 
93
        /* Test dw_zap_lws */
 
94
        c1 = dw_create(256);
 
95
        dw_qrappend((uint8_t *)" \t  test",c1,0);
 
96
        dw_stdout(c1);
 
97
        dw_destroy(c2);
 
98
        c2 = 0;
 
99
        c2 = dw_zap_lws(c1);
 
100
        dw_stdout(c2);
 
101
        dw_destroy(c2);
 
102
        c2 = 0;
 
103
 
 
104
        c1 = dw_create(5);
 
105
        printf("blank hash: %x\n",(unsigned int)dwh_hash_compress(c1));
 
106
        c1->len = 1; /* Don't do this in production code! */
 
107
        for(a = 0; a<16;a++) {
 
108
                *(c1->str) = a; /* Again, not in production code! */
 
109
                printf("hash of %d: %x\n",a,(unsigned int)dwh_hash_compress(c1));
 
110
        }
 
111
 
 
112
        /* Coding style requires that we always have the following part
 
113
         * at the end of any function that allocates strings */
 
114
 
 
115
catch_main:
 
116
        if(test != 0) {
 
117
                dw_destroy(test);
 
118
        }
 
119
        if(c1 != 0) {
 
120
                dw_destroy(c1);
 
121
        }
 
122
        if(c2 != 0) {
 
123
                dw_destroy(c2);
 
124
        }
 
125
        if(c3 != 0) {
 
126
                dw_destroy(c3);
 
127
        }
 
128
        return 0;
 
129
}