File: test_Base64.ml

package info (click to toggle)
extlib 1.7.7-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 632 kB
  • sloc: ml: 6,980; makefile: 128; sh: 42; ansic: 31
file content (48 lines) | stat: -rw-r--r-- 1,585 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
(*
 * ExtLib Testing Suite
 * Copyright (C) 2004 Janne Hellsten
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version,
 * with the special exception on linking described in file LICENSE.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *)

let in_range c a b = 
  let i = int_of_char c 
  and ai = int_of_char a 
  and bi = int_of_char b in
  (i >= ai && i <= bi)

let check_chars s =
  let len = String.length s in
  if len > 0 then
    begin
      for i = 0 to len-1 do
        let c = s.[i] in
        if not (in_range c 'A' 'Z') then
          if not (in_range c 'a' 'z') then
            if not (in_range c '0' '9') then
              assert (c = '/' || c = '+')
      done
    end

let () =
  Util.register1 "Base64" "random"
    (fun () -> 
       for i = 0 to 64 do
         let s = Util.random_string () in
         let enc = Base64.encode_string s in
         assert ((Base64.decode_string enc) = s);
         check_chars enc
       done)