Blame test/test_ExtString.ml

rpm-build 0f2925
(*
rpm-build 0f2925
 * ExtLib Testing Suite
rpm-build 0f2925
 * Copyright (C) 2004 Janne Hellsten
rpm-build 0f2925
 * Copyright (C) 2011 ygrek
rpm-build 0f2925
 *
rpm-build 0f2925
 * This library is free software; you can redistribute it and/or
rpm-build 0f2925
 * modify it under the terms of the GNU Lesser General Public
rpm-build 0f2925
 * License as published by the Free Software Foundation; either
rpm-build 0f2925
 * version 2.1 of the License, or (at your option) any later version,
rpm-build 0f2925
 * with the special exception on linking described in file LICENSE.
rpm-build 0f2925
 *
rpm-build 0f2925
 * This library is distributed in the hope that it will be useful,
rpm-build 0f2925
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 0f2925
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build 0f2925
 * Lesser General Public License for more details.
rpm-build 0f2925
 *
rpm-build 0f2925
 * You should have received a copy of the GNU Lesser General Public
rpm-build 0f2925
 * License along with this library; if not, write to the Free Software
rpm-build 0f2925
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
rpm-build 0f2925
 *)
rpm-build 0f2925
rpm-build 0f2925
open ExtString
rpm-build 0f2925
rpm-build 0f2925
module S = String
rpm-build 0f2925
rpm-build 0f2925
let t_starts_with () = 
rpm-build 0f2925
  let s0 = "foo" in
rpm-build 0f2925
  assert (S.starts_with s0 s0);
rpm-build 0f2925
  assert (S.starts_with s0 "f");
rpm-build 0f2925
  assert (not (S.starts_with s0 "bo"));
rpm-build 0f2925
  assert (not (S.starts_with "" "foo"));
rpm-build 0f2925
  assert (S.starts_with s0 "");
rpm-build 0f2925
  assert (S.starts_with "" "")
rpm-build 0f2925
rpm-build 0f2925
let t_ends_with () = 
rpm-build 0f2925
  let s0 = "foo" in
rpm-build 0f2925
  assert (S.ends_with s0 "foo");
rpm-build 0f2925
  assert (S.ends_with s0 "oo");
rpm-build 0f2925
  assert (S.ends_with s0 "o");
rpm-build 0f2925
  assert (S.ends_with s0 "");
rpm-build 0f2925
  assert (S.ends_with "" "");
rpm-build 0f2925
  assert (not (S.ends_with "" "b"));
rpm-build 0f2925
  assert (not (S.ends_with s0 "f"))
rpm-build 0f2925
rpm-build 0f2925
let t_map () =
rpm-build 0f2925
  let s0 = "foobar" in
rpm-build 0f2925
  assert (S.map Std.identity s0 = s0)
rpm-build 0f2925
rpm-build 0f2925
let t_lchop () =
rpm-build 0f2925
  for len = 0 to 15 do
rpm-build 0f2925
    let s0 = Util.random_string_len len in
rpm-build 0f2925
    let s0len = String.length s0
rpm-build 0f2925
    and s0r = ref (String.copy s0) in
rpm-build 0f2925
    for i = 0 to s0len-1 do
rpm-build 0f2925
      assert (!s0r.[0] = s0.[i]);
rpm-build 0f2925
      s0r := String.lchop !s0r
rpm-build 0f2925
    done;
rpm-build 0f2925
  done
rpm-build 0f2925
rpm-build 0f2925
let t_rchop () =
rpm-build 0f2925
  for len = 0 to 15 do
rpm-build 0f2925
    let s0 = Util.random_string_len len in
rpm-build 0f2925
    let s0len = String.length s0
rpm-build 0f2925
    and s0r = ref (String.copy s0) in
rpm-build 0f2925
    for i = 0 to s0len-1 do
rpm-build 0f2925
      assert (!s0r.[String.length !s0r - 1] = s0.[s0len-1-i]);
rpm-build 0f2925
      s0r := String.rchop !s0r
rpm-build 0f2925
    done;
rpm-build 0f2925
  done
rpm-build 0f2925
rpm-build 0f2925
let t_split () = 
rpm-build 0f2925
  for i = 0 to 64 do
rpm-build 0f2925
    let s = Util.random_string () in
rpm-build 0f2925
    let s' = String.replace_chars 
rpm-build 0f2925
               (fun c -> if c = '|' then "_" else String.of_char c) s in
rpm-build 0f2925
    let len = String.length s' in
rpm-build 0f2925
    if len > 0 then
rpm-build 0f2925
      begin
rpm-build 0f2925
        let rpos = Random.int len in
rpm-build 0f2925
        (* Insert separator and split based on that *)
rpm-build 0f2925
        let modified =
rpm-build 0f2925
          let b = Bytes.of_string s' in
rpm-build 0f2925
          b.[rpos] <- '|';
rpm-build 0f2925
          Bytes.to_string b
rpm-build 0f2925
        in
rpm-build 0f2925
        let (half1, half2) = String.split modified "|" in
rpm-build 0f2925
        if rpos > 1 then
rpm-build 0f2925
          begin
rpm-build 0f2925
            assert (String.length half1 = rpos);
rpm-build 0f2925
            assert (String.sub s' 0 rpos = half1)
rpm-build 0f2925
          end;
rpm-build 0f2925
        if rpos < len-1 then
rpm-build 0f2925
          begin
rpm-build 0f2925
            assert (String.length half2 = len-rpos-1);
rpm-build 0f2925
            assert (String.sub s' (rpos+1) (len-rpos-1) = half2);
rpm-build 0f2925
          end;
rpm-build 0f2925
        assert (String.join "|" [half1; half2] = modified);
rpm-build 0f2925
      end
rpm-build 0f2925
  done
rpm-build 0f2925
rpm-build 0f2925
let t_replace1 () =
rpm-build 0f2925
  let s = "karhupullo" in
rpm-build 0f2925
  assert (String.replace s "karhu" "kalja" = (true, "kaljapullo"));
rpm-build 0f2925
  assert (String.replace s "kalja" "karhu" = (false, s));
rpm-build 0f2925
  (* TODO is this correct?  Is "" supposed to always match? *)
rpm-build 0f2925
  assert (String.replace s "" "karhu" = (true, "karhu"^s));
rpm-build 0f2925
  assert (String.replace "" "" "karhu" = (true, "karhu"))
rpm-build 0f2925
rpm-build 0f2925
let t_strip () = 
rpm-build 0f2925
  let s = "1234abcd5678" in
rpm-build 0f2925
  assert (S.strip ~chars:"" s = s);
rpm-build 0f2925
  assert (S.strip ~chars:"1" s = String.sub s 1 (String.length s-1));
rpm-build 0f2925
  assert (S.strip ~chars:"12" s = String.sub s 2 (String.length s-2));
rpm-build 0f2925
  assert (S.strip ~chars:"1234" s = "abcd5678");
rpm-build 0f2925
  assert (S.ends_with (S.strip ~chars:"8" s) "567");
rpm-build 0f2925
  assert (S.ends_with (S.strip ~chars:"87" s) "56");
rpm-build 0f2925
  assert (S.ends_with (S.strip ~chars:"86" s) "567");
rpm-build 0f2925
  assert (S.ends_with (S.strip ~chars:"" s) "5678")
rpm-build 0f2925
rpm-build 0f2925
let t_nsplit () =
rpm-build 0f2925
  let s = "testsuite" in
rpm-build 0f2925
  assert (S.nsplit s "t" = ["";"es";"sui";"e"]);
rpm-build 0f2925
  assert (S.nsplit s "te" = ["";"stsui";""]);
rpm-build 0f2925
  assert (try let _ = S.nsplit s "" in false with Invalid_string -> true)
rpm-build 0f2925
rpm-build 0f2925
let () = 
rpm-build 0f2925
  Util.register "ExtString" [
rpm-build 0f2925
    "starts_with", t_starts_with;
rpm-build 0f2925
    "ends_with", t_ends_with;
rpm-build 0f2925
    "map", t_map;
rpm-build 0f2925
    "lchop", t_lchop;
rpm-build 0f2925
    "rchop", t_rchop;
rpm-build 0f2925
    "split", t_split;
rpm-build 0f2925
    "replace_1", t_replace1;
rpm-build 0f2925
    "strip", t_strip;
rpm-build 0f2925
    "nsplit", t_nsplit;
rpm-build 0f2925
  ]