~ubuntu-branches/ubuntu/saucy/numactl/saucy-proposed

« back to all changes in this revision

Viewing changes to test/tbitmap.c

  • Committer: Bazaar Package Importer
  • Author(s): Ian Wienand
  • Date: 2006-11-03 10:31:24 UTC
  • mfrom: (1.2.1 upstream) (3.1.3 edgy)
  • Revision ID: james.westby@ubuntu.com-20061103103124-9dy65897wxq3g7ku
add powerpc.patch; fix FTBFS due to typo on PowerPC

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Unit test bitmap parser */
 
2
#define _GNU_SOURCE 1
 
3
//#include <asm/bitops.h>
 
4
#include <stdio.h>
 
5
#include <string.h>
 
6
#include <assert.h>
 
7
#include <stdlib.h>
 
8
#include <ctype.h>
 
9
 
 
10
#define ALIGN(x,a) (((x)+(a)-1)&~((a)-1))
 
11
 
 
12
#define test_bit(i,p)  ((p)[(i) / BITS_PER_LONG] &   (1UL << ((i)%BITS_PER_LONG)))
 
13
#define set_bit(i,p)   ((p)[(i) / BITS_PER_LONG] |=  (1UL << ((i)%BITS_PER_LONG)))
 
14
#define clear_bit(i,p) ((p)[(i) / BITS_PER_LONG] &= ~(1UL << ((i)%BITS_PER_LONG)))
 
15
 
 
16
 
 
17
typedef unsigned u32;
 
18
#define BITS_PER_LONG (sizeof(long)*8)
 
19
 
 
20
#define round_up(x,y) (((x) + (y) - 1) & ~((y)-1))
 
21
 
 
22
#define CPU_BYTES(x) (round_up(x, BITS_PER_LONG)/8)
 
23
#define CPU_LONGS(x) (CPU_BYTES(x) / sizeof(long))
 
24
 
 
25
/* Following routine extracted from Linux 2.6.16 */
 
26
 
 
27
#define CHUNKSZ                         32
 
28
#define nbits_to_hold_value(val)        fls(val)
 
29
#define unhex(c)                        (isdigit(c) ? (c - '0') : (toupper(c) - 'A' + 10))
 
30
#define BASEDEC 10              /* fancier cpuset lists input in decimal */
 
31
 
 
32
/**
 
33
 * bitmap_scnprintf - convert bitmap to an ASCII hex string.
 
34
 * @buf: byte buffer into which string is placed
 
35
 * @buflen: reserved size of @buf, in bytes
 
36
 * @maskp: pointer to bitmap to convert
 
37
 * @nmaskbits: size of bitmap, in bits
 
38
 *
 
39
 * Exactly @nmaskbits bits are displayed.  Hex digits are grouped into
 
40
 * comma-separated sets of eight digits per set.
 
41
 */
 
42
int bitmap_scnprintf(char *buf, unsigned int buflen,
 
43
        const unsigned long *maskp, int nmaskbits)
 
44
{
 
45
        int i, word, bit, len = 0;
 
46
        unsigned long val;
 
47
        const char *sep = "";
 
48
        int chunksz;
 
49
        u32 chunkmask;
 
50
 
 
51
        chunksz = nmaskbits & (CHUNKSZ - 1);
 
52
        if (chunksz == 0)
 
53
                chunksz = CHUNKSZ;
 
54
 
 
55
        i = ALIGN(nmaskbits, CHUNKSZ) - CHUNKSZ;
 
56
        for (; i >= 0; i -= CHUNKSZ) {
 
57
                chunkmask = ((1ULL << chunksz) - 1);
 
58
                word = i / BITS_PER_LONG;
 
59
                bit = i % BITS_PER_LONG;
 
60
                val = (maskp[word] >> bit) & chunkmask;
 
61
                len += snprintf(buf+len, buflen-len, "%s%0*lx", sep,
 
62
                        (chunksz+3)/4, val);
 
63
                chunksz = CHUNKSZ;
 
64
                sep = ",";
 
65
        }
 
66
        return len;
 
67
}
 
68
 
 
69
extern int numa_parse_bitmap(char  *buf,unsigned long *mask, unsigned bits);
 
70
 
 
71
int main(void)
 
72
{
 
73
        char buf[1024];
 
74
        unsigned long mask[20], mask2[20];
 
75
        int i;
 
76
        printf("Testing bitmap functions\n");
 
77
        for (i = 0; i < 300; i++) {
 
78
                memset(mask, 0, sizeof(mask));
 
79
                memset(mask2, 0, sizeof(mask));
 
80
                set_bit(i, mask);
 
81
                bitmap_scnprintf(buf, sizeof(buf), mask, 300);
 
82
                strcat(buf,"\n");
 
83
                if (numa_parse_bitmap(buf, mask2, 300) < 0)
 
84
                        assert(0);
 
85
                if (memcmp(mask, mask2, sizeof(mask))) { 
 
86
                        bitmap_scnprintf(buf, sizeof(buf), mask2, 300); 
 
87
                        printf("mask2 differs: %s\n", buf);
 
88
                        assert(0);
 
89
                }
 
90
        }
 
91
        printf("Passed\n");
 
92
        return 0;
 
93
}
 
94