|
| 1 | +(***********************************************************************) |
| 2 | +(* *) |
| 3 | +(* OCaml *) |
| 4 | +(* *) |
| 5 | +(* Valerie Menissier-Morain, projet Cristal, INRIA Rocquencourt *) |
| 6 | +(* *) |
| 7 | +(* Copyright 1996 Institut National de Recherche en Informatique et *) |
| 8 | +(* en Automatique. All rights reserved. This file is distributed *) |
| 9 | +(* under the terms of the Q Public License version 1.0. *) |
| 10 | +(* *) |
| 11 | +(***********************************************************************) |
| 12 | + |
| 13 | +open Printf;; |
| 14 | + |
| 15 | +let flush_all () = flush stdout; flush stderr;; |
| 16 | + |
| 17 | +let message s = print_string s; print_newline ();; |
| 18 | + |
| 19 | +let error_occurred = ref false;; |
| 20 | +let immediate_failure = ref true;; |
| 21 | + |
| 22 | +let error () = |
| 23 | + if !immediate_failure then exit 2 else begin |
| 24 | + error_occurred := true; |
| 25 | + flush_all (); |
| 26 | + false |
| 27 | + end;; |
| 28 | + |
| 29 | +let success () = flush_all (); true;; |
| 30 | + |
| 31 | +let function_tested = ref "";; |
| 32 | + |
| 33 | +let testing_function s = |
| 34 | + flush_all (); |
| 35 | + function_tested := s; |
| 36 | + print_newline(); |
| 37 | + message s;; |
| 38 | + |
| 39 | +let test test_number eq_fun (answer, correct_answer) = |
| 40 | + flush_all (); |
| 41 | + if not (eq_fun answer correct_answer) then begin |
| 42 | + fprintf stderr ">>> Bad result (%s, test %d)\n" !function_tested test_number; |
| 43 | + error () |
| 44 | + end else begin |
| 45 | + printf " %d..." test_number; |
| 46 | + success () |
| 47 | + end;; |
| 48 | + |
| 49 | +let failure_test test_number fun_to_test arg = |
| 50 | + flush_all (); |
| 51 | + try |
| 52 | + fun_to_test arg; |
| 53 | + fprintf stderr ">>> Failure expected (%s, test %d)\n" |
| 54 | + !function_tested test_number; |
| 55 | + error () |
| 56 | + with _ -> |
| 57 | + printf " %d..." test_number; |
| 58 | + success ();; |
| 59 | + |
| 60 | +let failwith_test test_number fun_to_test arg correct_failure = |
| 61 | + flush_all (); |
| 62 | + try |
| 63 | + fun_to_test arg; |
| 64 | + fprintf stderr ">>> Failure expected (%s, test %d)\n" |
| 65 | + !function_tested test_number; |
| 66 | + error () |
| 67 | + with x -> |
| 68 | + if x = correct_failure then begin |
| 69 | + printf " %d..." test_number; |
| 70 | + success () |
| 71 | + end else begin |
| 72 | + fprintf stderr ">>> Bad failure (%s, test %d)\n" |
| 73 | + !function_tested test_number; |
| 74 | + error () |
| 75 | + end;; |
| 76 | + |
| 77 | +let end_tests () = |
| 78 | + flush_all (); |
| 79 | + print_newline (); |
| 80 | + if !error_occurred then begin |
| 81 | + print_endline "************* TESTS FAILED ****************"; exit 2 |
| 82 | + end else begin |
| 83 | + print_endline "************* TESTS COMPLETED SUCCESSFULLY ****************"; |
| 84 | + exit 0 |
| 85 | + end;; |
| 86 | + |
| 87 | +let eq = (==);; |
| 88 | +let eq_int (i: int) (j: int) = (i = j);; |
| 89 | +let eq_string (i: string) (j: string) = (i = j);; |
| 90 | +let eq_nativeint (i: nativeint) (j: nativeint) = (i = j);; |
| 91 | +let eq_int32 (i: int32) (j: int32) = (i = j);; |
| 92 | +let eq_int64 (i: int64) (j: int64) = (i = j);; |
| 93 | + |
| 94 | +let sixtyfour = (1 lsl 31) <> 0;; |
| 95 | + |
| 96 | +let rec gcd_int i1 i2 = |
| 97 | + if i2 = 0 then abs i1 else gcd_int i2 (i1 mod i2);; |
| 98 | + |
| 99 | +let rec num_bits_int_aux n = |
| 100 | + if n = 0 then 0 else succ(num_bits_int_aux (n lsr 1));; |
| 101 | + |
| 102 | +let num_bits_int n = num_bits_int_aux (abs n);; |
| 103 | + |
| 104 | +let sign_int i = if i = 0 then 0 else if i > 0 then 1 else -1;; |
| 105 | + |
| 106 | +let length_of_int = Sys.word_size - 2;; |
| 107 | + |
| 108 | +let monster_int = 1 lsl length_of_int;; |
| 109 | +let biggest_int = monster_int - 1;; |
| 110 | +let least_int = - biggest_int;; |
| 111 | + |
| 112 | +let compare_int n1 n2 = |
| 113 | + if n1 == n2 then 0 else if n1 > n2 then 1 else -1;; |
0 commit comments