1 /******************************************************************************* 2 3 Array manipulation functions. 4 5 It's often convenient to use these functions with D's 'function as array 6 property' syntax, so: 7 8 --- 9 mstring dest; 10 concat(dest, "hello ", "world"); 11 --- 12 13 could also be written as: 14 15 --- 16 mstring dest; 17 dest.concat("hello ", "world"); 18 --- 19 20 TODO: Extend unittest to test all functions in this module. 21 22 Copyright: 23 Copyright (c) 2009-2016 dunnhumby Germany GmbH. 24 All rights reserved. 25 26 License: 27 Boost Software License Version 1.0. See LICENSE_BOOST.txt for details. 28 Alternatively, this file may be distributed under the terms of the Tango 29 3-Clause BSD License (see LICENSE_BSD.txt for details). 30 31 *******************************************************************************/ 32 33 module ocean.core.Array; 34 35 36 import ocean.meta.types.Qualifiers; 37 38 import ocean.core.Test; 39 import ocean.core.Buffer; 40 41 public import ocean.core.array.Mutation; 42 public import ocean.core.array.Transformation; 43 public import ocean.core.array.Search; 44 45 /******************************************************************************* 46 47 Alias to keep backwards compatibility with Array module in v1.x.x 48 49 *******************************************************************************/ 50 51 public alias ocean.core.array.Transformation.remove remove; 52 53 /******************************************************************************* 54 55 Unittest 56 57 *******************************************************************************/ 58 59 unittest 60 { 61 Buffer!(char) str; 62 test (copy(str, "Die Katze tritt die Treppe krumm.") == "Die Katze tritt die Treppe krumm."); 63 64 str.length = 0; 65 test (concat(str, "Die "[], "Katze "[], "tritt "[], "die "[], "Treppe "[], "krumm."[]) == "Die Katze tritt die Treppe krumm."); 66 67 mstring nothing = null; 68 69 str.length = 0; 70 test (concat(str, "Die "[], ""[], "Katze "[], "tritt "[], nothing, "die "[], "Treppe "[], "krumm."[]) == "Die Katze tritt die Treppe krumm."); 71 72 str.length = 0; 73 append(str, "Die Katze "[]); 74 test (str[] == "Die Katze "); 75 append(str, "tritt "[], "die "[]); 76 test (append(str, "Treppe "[], "krumm."[]) == "Die Katze tritt die Treppe krumm."); 77 78 long[] arr = [1, 2, 3, 5, 8, 13, 21]; 79 80 size_t n; 81 82 test(bsearch(arr, 5L, n)); 83 test!("==")(n, 3); 84 85 test(bcontains(arr, 5L)); 86 } 87 88 version (unittest) 89 { 90 91 // Tests string concatenation function against results of the normal ~ operator 92 bool concat_test ( T... ) ( T strings ) 93 { 94 Buffer!(char) dest; 95 concat(dest, strings); 96 97 mstring concat_result; 98 foreach ( str; strings ) 99 { 100 concat_result ~= str; 101 } 102 return dest[] == concat_result ; 103 } 104 } 105 106 unittest 107 { 108 mstring dest; 109 istring str1 = "hello"; 110 istring str2 = "world"; 111 istring str3 = "something"; 112 113 // Check dynamic array concatenation 114 test(concat_test(dest, str1, str2, str3), "Concatenation test failed"); 115 116 // Check that modifying one of the concatenated strings doesn't modify the result 117 mstring result = dest.dup; 118 str1 = "goodbye"; 119 test!("==")(dest, result); 120 121 // Check null concatenation 122 test(concat_test(dest), "Null concatenation test 1 failed"); 123 test(concat_test(dest, "", ""), "Null concatenation test 2 failed"); 124 125 // Check static array concatenation 126 char[3] staticstr1 = "hi "; 127 char[5] staticstr2 = "there"; 128 test(concat_test(dest, staticstr1, staticstr2), "Static array concatenation test failed"); 129 130 // Check manifest constant array concatenation 131 static immutable conststr1 = "hi "; 132 static immutable conststr2 = "there"; 133 test(concat_test(dest, conststr1, conststr2), "Const array concatenation test failed"); 134 } 135 136 unittest 137 { 138 // Check there is not function clashes calling remove() from this module. 139 // The remove() function defined in ocean.core.array.Transformation is 140 // expected to be the default. 141 int[] array; 142 remove([0, 1, 2, 2, 2, 3, 4], [2], array); 143 test!("==")(array, [0, 1, 3, 4], "Array does not match"); 144 }