File: unittest.cpp

package info (click to toggle)
wham-align 0.1.5-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 892 kB
  • sloc: cpp: 8,769; sh: 76; makefile: 52
file content (207 lines) | stat: -rw-r--r-- 4,313 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "bitread.h"
#include "sequence.h"
#include "error.h"

void genRandomSequence(char * str, int len) {
  for (int i = 0; i < len; i++) {
    switch (rand() % 4) {
    case 0:
      str[i] = 'A';
      break;
    case 1:
      str[i] = 'C';
      break;
    case 2:
      str[i] = 'G';
      break;
    case 3:
      str[i] = 'T';
      break;
    }
  }
}
bool testExtract() {
  int len, offset;
  int i, j;
  char str[1024], str1[256];
  char * fname[1];
  int64 space[16];
  int64 * key = &space[8];
  int ret;

  fname[1] = new char[128];
  strcpy(fname[1], "./tmp_test.fa");

  genRandomSequence(str, 1000);

  FILE * file = fopen(fname[1], "w");
  for (i = 0; i < 20; i++) {
    for (j = 0; j < 50; j++) {
      fprintf(file, "%c", str[i * 50 + j]);
    }
    fprintf(file, "\n");
  }
  fclose(file);

  CompactSequence * sequence = new CompactSequence(true);
  sequence->build(fname, 1, 36, 2);
  if (ret != SUCCESS)
  {
    printf("Failed to load reference sequences.\n");
    return false;
  }

  int64 * seq = sequence->getSequence();

  printf("Testing extract... ");

  for (len = 36; len <= 36; len++) {
    offset = rand() % 500;
    BitRead::extract(seq, key, offset * BITS_PER_BASE, len * BITS_PER_BASE);

    CompactSequence::decompose(str1, len, key);

    for (i = 0; i < len; i++) {
      if (str[i + offset] != str1[i]) {
        printf("Failed at %d bps\n", len);
        return false;
      }
    }
  }

  printf("Passed\n");
  return true;
}

bool testRemoveHead() {
  int len;
  int head, i;
  char str1[256], str2[256];
  int64 space1[16], space2[16];
  int64 mask[WORDS_PER_READ];
  int64 * key1, *key2;

  key1 = &space1[8];
  key2 = &space2[8];

  printf("Testing remove head... ");

  for (len = 36; len <= 128; len++) {

    genRandomSequence(str1, len);

    CompactSequence::compose(str1, len, key1);

    for (head = len - 10; head < len; head++) {

      BitRead::genHeadMask(mask, head * BITS_PER_BASE);

      BitRead::removeHead(key1, key2, mask);

      CompactSequence::decompose(str2, head, key2);
      /*
       printf("\n");
       for (i = 0; i < len; i++)
       printf("%c", str1[i]);
       printf("\n");

       for (i = 0; i < len - head; i++)
       printf(" ");
       for (i = 0; i < head; i++)
       printf("%c", str2[i]);
       printf("\n");
       */
      //check
      for (i = 0; i < head; i++) {
        if (str1[i + len - head] != str2[i]) {
          printf("Failed at %d bps, %d head\n", len, head);
          return false;
        }
      }
    }
  }

  printf("Passed\n");
  return true;
}

bool testRemoveInterval() {
  int len;
  int p, offset, i;
  char str1[256], str2[256];
  int64 space1[16], space2[16];
  int64 * key1, *key2;

  key1 = &space1[8];
  key2 = &space2[8];

  printf("Testing remove interval... ");

  for (len = 36; len <= 128; len++) {

    genRandomSequence(str1, len);

    CompactSequence::compose(str1, len, key1);

    for (p = 2; p <= 8; p++) {

      for (offset = 0; offset < len / p * p; offset += len / p) {

        BitRead::removeInterval(key1, key2, offset * BITS_PER_BASE
            , len / p * BITS_PER_BASE);

        CompactSequence::decompose(str2, len - len / p, key2);
        /*
         for (i = 0; i < len; i++)
         printf("%c", str1[i]);
         printf("\n");

         for (i = 0; i < len - offset - len/p; i++)
         printf("%c", str2[i]);
         for (i = 0; i < len/p; i++)
         printf(" ");
         for (i = len - offset - len/p; i < len - len/p; i++)
         printf("%c", str2[i]);
         printf("\n\n");
         */

        //check
        for (i = 0; i < len - offset - len / p; i++) {
          if (str1[i] != str2[i]) {
            printf("Failed at %d bps, %d partition, %d offset\n", len, p,
                offset);
            return false;
          }
        }

        for (i = len - offset - len / p; i < len - len / p; i++) {
          if (str1[i + len / p] != str2[i]) {
            printf("Failed at %d bps, %d partition, %d offset\n", len, p,
                offset);
            return false;
          }
        }

      }
    }
  }

  printf("Passed\n");
  return true;
}

int main() {
  int i = 1;
  int j = 2;

//	testExtract();

  testRemoveHead();

  testRemoveInterval();

  return 0;
}