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
  
START_MARKER 
cond_begin([[ master()->resolv("Bz2")->Deflate ]]) 
 
test_true([[Bz2.Inflate();]]) 
define(test_codec,[[ 
  test_eq([[Bz2.Deflate()->finish($1)]],[[$2]]) 
  test_eq([[Bz2.Inflate()->inflate($2)]],[[$1]]) 
]]) 
 
dnl Some strings can be compressed to different strings, 
dnl depending on Bz2 version. Test for both, then. 
 
define(test_codec2,[[ 
  test_any([[ 
     string uncompressed=$1; 
     array(string) vcompressed=$2; 
     string result, result2; 
 
     foreach (vcompressed;;string compressed) 
       if ((result=Bz2.Inflate()->inflate(compressed))!=uncompressed) 
         error("Bz2 inflate failed:\n%O -> %O, expected %O\n", 
               compressed,result,uncompressed); 
 
     result=Bz2.Deflate()->finish(uncompressed); 
 
     if ((result2=Bz2.Inflate()->inflate(result))!=uncompressed) 
        error("Bz2 deflate not reversible:\n%O -> %O -> %O\n", 
              uncompressed, result, result2); 
 
     if (sizeof(result) > 0.75*sizeof(uncompressed)) 
        error("Bz2 deflation insufficient:\n%O -> %O == %.1f%%\n", 
              uncompressed, result, 100.0*sizeof(result)/sizeof(uncompressed)); 
 
//       if (search(vcompressed,result)==-1) 
//          error("Bz2 deflate failed:\n%O -> %O, expected any of %O\n", 
//            uncompressed,result,vcompressed); 
 
     return 1; 
   ]],1) 
]]) 
test_codec([[""]],[["BZh9\27rE8P\220\0\0\0\0"]]) 
test_codec([["x"]],[["BZh91AY&SYwK\260\24\0\0\0\0\200\0@ \0!\30F\202\356H\247\n\22\16\351v\2\200"]]) 
test_codec2([["x"*10000]],[[ 
 ({MIME.decode_base64("QlpoOTFBWSZTWbgIP9SAABWIgIgAAEAACCAAMMwFKacao2KjxdyRThQkLgIP9QA="), 
   MIME.decode_base64("QlpoOTFBWSZTWbgIP9QAABWIgIgAAEAACCAAMMwFKacao2KjxdyRThQkLgIP9QA=") 
 })]]) 
test_codec("x\0x",[["BZh91AY&SY\320Vo\310\0\0\0\300\200@\0\0@ \0!\0\202,]\311\24\341BCAY\277 "]]) 
test_codec2([[(string)enumerate(256)*3]],[[({ 
MIME.decode_base64("QlpoOTFBWSZTWe3ga8yAAAF/////////////////////////////////////////////sAFgAS//9VVIAAAAAAAAAAAAAAAAAAAAEwmAAAAAAAAAAAAAAAAAABMAiTAATAAEwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAASYACYAAmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH9QCBoIgqDIOhCEoUhaGIahyHogiKJImiiKosi6MIyjSNo4jqPI+kCQpEkaSJKkyTpQlKVJWliWpcl6YJimSZpomqbJunCcp0naeJ6nyfqAoKhKGoiiqMo6kKSpSlqYpqnKeqCoqkqaqKqqyrqwrKtK2riuq8r6wLCsSxrIsqzLOtC0rUtcCgLYtq3LeuC4rkua6Lquy7rwvK9L2vi+r8v7/GAYFgmDf6wjCsMw7EMSxTFsYxrHMeyDIskybKMqyzLswzLNM2zjOs8z7QNC0TRtI0rTNO1DUtU1bWNa1zXtg2LZNm2jats27cNy3Tdt43rfN+4DguE4biOK4zjuQ5LlOW5jmuc57oOi6Tpuo6rrOu7Dsu07buO67zvvA8LxPG8jyvM870PS9T1vY9r3Pe+D4vk+b6Pq/59n3fh+X6ft/H9f+LuSKcKEh28DXmAA=="), 
MIME.decode_base64("QlpoOTFBWSZTWe3ga8wAAAF/////////////////////////////////////////////sAFgAS//9VVQJgJgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACTAATAAEwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAASYACYAAmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH9QFAkDQRBX/IMg6EIShSFoY/9DUOQ9EERRJE0URVFkXRhGUaRtHEdR5H0gSFIkjSRJUmSdKEpSpK0sS1LkvTBMUyTNNE1TZN04TlOk7TxPU+T9QFBUJQ1EUVRlHUhSVKUtTFNU5T1QVFUlTVRVVWVdWFZVpW1cV1XlfWBYViWNZFlWZZ1oWlalrWxbVuW9cFxXJc10XVdl3XheV6XtfF9X5f2AYFgmDYRhWGYdiGJYpi2MY1jmPZBkWSZNlGVZZl2YZlmmbZxnWeZ9oGhaJo2kaVpmnahqWqatrGta5r2wbFsmzbRtW2bduG5bpu28b1vm/cBwXCcNxHFcZx3IclynLcxzXOc90HRdJ03UdV1nXdh2Xadt3Hdd533geF4njeR5Xmed6Hpep63se17nvfB8XyfN9H1fZ934fl+n7fx/X+F3JFOFCQ7eBrzAA=="), 
MIME.decode_base64("QlpoOTFBWSZTWe3ga8yAAAF/////////////////////////////////////////////wAFgAS//9VVIAAAAAAAAAAAAAAAAAAAAEwmAAAAAAAAAAAAAAAAAABMAiTAATAAEwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAASYACYAAmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACTAATAAEwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/qAQNBEFQZB0IQlCkLQxDUOQ9EERRJE0URVFkXRhGUaRtHEdR5H0gSFIkjSRJUmSdKEpSpK0sS1LkvTBMUyTNNE1TZN04TlOk7TxPU+T9QFBUJQ1EUVRlHUhSVKUtTFNU5T1QVFUlTVRVVWVdWFZVpW1cV1XlfWBYViWNZFlWZZ1oWlalrgUBbFtW5b1wXFclzXRdV2XdeF5Xpe18X1fl/f4wDAsEwb/WEYVhmHYhiWKYtjGNY5j2QZFkmTZRlWWZdmGZZpm2cZ1nmfaBoWiaNpGlaZp2oalqmraxrWua9sGxbJs20bVtm3bhuW6btvG9b5v3AcFwnDcRxXGcdyHJcpy3Mc1znPdB0XSdN1HVdZ13Ydl2nbdx3Xed94HheJ43keV5nneh6Xqet7Hte573wfF8nzfR9X/Ps+78Py/T9v4/r/xdyRThQkO3ga8wA=") 
})]]) 
 
test_eq([[Bz2.BZ_RUN]],0) 
test_eq([[Bz2.BZ_FLUSH]],1) 
test_eq([[Bz2.BZ_FINISH]],2) 
 
test_any([[ 
 
  string packed = ""; 
  string in_data = ""; 
  string out_data = ""; 
  Bz2.Deflate defl = Bz2.Deflate(); 
  Bz2.Inflate infl = Bz2.Inflate(); 
  int defl_chunk_size = 75000; 
  int infl_chunk_size = 39324; 
  int flush_intervall = 7; 
  int i = 0; 
  int j = 0; 
  int flush_count = 0; 
 
  in_data = random_string(100000); 
 
  while(i + defl_chunk_size < sizeof(in_data)){ 
    if(flush_count != 7){ 
      defl->feed(in_data[i..i + defl_chunk_size - 1]); 
      flush_count++; 
    } 
    else{ 
      packed += defl->read(in_data[i..i + defl_chunk_size - 1]); 
      flush_count = 0; 
    } 
    i += defl_chunk_size; 
  } 
 
  packed += defl->finish(in_data[i..sizeof(in_data) - 1]); 
 
  __signal_watchdog(); 
 
  while(j + infl_chunk_size < sizeof(packed)) 
  { 
    out_data += infl->inflate(packed[j..j + infl_chunk_size - 1]); 
    j += infl_chunk_size; 
  } 
  out_data += infl->inflate(packed[j..sizeof(packed)-1]); 
 
  return(out_data == in_data); 
 
]],1) 
 
cond_end // Bz2.Deflate 
 
cond_begin([[ master()->resolv("Bz2")->File ]]) 
 
test_any([[ 
 
  string in_data = ""; 
  string out_data = ""; 
  Bz2.File write_file = Bz2.File(); 
  Bz2.File read_file = Bz2.File(); 
 
  in_data = "jglaksdjalsdkfjwoeiruwoiurbenrbwenrwikerhwkeurwhfksnASDFASJFKSDJFASIGHSFGAFD34535234523426FSFGSFDGSDRHTWGDASDFASDF"; 
  in_data = in_data * 1000;     // ~115k 
 
  write_file->write_open("test_data_tmp.bz2"); 
  write_file->write(in_data); 
  write_file->close(); 
 
  __signal_watchdog(); 
 
  read_file->read_open("test_data_tmp.bz2"); 
  out_data = read_file->read(); 
  string empty = read_file->read(); 
  if( empty!="" ) return 0; 
  read_file->close(); 
  Stdio.recursive_rm("test_data_tmp.bz2"); 
  return(in_data == out_data); 
 
]],1) 
 
cond_end // Bz2.File 
 
END_MARKER