js/src/tests/js1_2/Array/slice.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/js1_2/Array/slice.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,89 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +/**
    1.11 +   Filename:     slice.js
    1.12 +   Description:  'This tests out some of the functionality on methods on the Array objects'
    1.13 +
    1.14 +   Author:       Nick Lerissa
    1.15 +   Date:         Fri Feb 13 09:58:28 PST 1998
    1.16 +*/
    1.17 +
    1.18 +var SECTION = 'As described in Netscape doc "Whats new in JavaScript 1.2"';
    1.19 +var VERSION = 'no version';
    1.20 +startTest();
    1.21 +var TITLE = 'String:slice';
    1.22 +
    1.23 +writeHeaderToLog('Executing script: slice.js');
    1.24 +writeHeaderToLog( SECTION + " "+ TITLE);
    1.25 +
    1.26 +var a = ['a','test string',456,9.34,new String("string object"),[],['h','i','j','k']];
    1.27 +var b = [1,2,3,4,5,6,7,8,9,0];
    1.28 +
    1.29 +exhaustiveSliceTest("exhaustive slice test 1", a);
    1.30 +exhaustiveSliceTest("exhaustive slice test 2", b);
    1.31 +
    1.32 +test();
    1.33 +
    1.34 +function mySlice(a, from, to)
    1.35 +{
    1.36 +  var from2       = from;
    1.37 +  var to2         = to;
    1.38 +  var returnArray = [];
    1.39 +  var i;
    1.40 +
    1.41 +  if (from2 < 0) from2 = a.length + from;
    1.42 +  if (to2 < 0)   to2   = a.length + to;
    1.43 +
    1.44 +  if ((to2 > from2)&&(to2 > 0)&&(from2 < a.length))
    1.45 +  {
    1.46 +    if (from2 < 0)        from2 = 0;
    1.47 +    if (to2 > a.length) to2 = a.length;
    1.48 +
    1.49 +    for (i = from2; i < to2; ++i) returnArray.push(a[i]);
    1.50 +  }
    1.51 +  return returnArray;
    1.52 +}
    1.53 +
    1.54 +// This function tests the slice command on an Array
    1.55 +// passed in. The arguments passed into slice range in
    1.56 +// value from -5 to the length of the array + 4. Every
    1.57 +// combination of the two arguments is tested. The expected
    1.58 +// result of the slice(...) method is calculated and
    1.59 +// compared to the actual result from the slice(...) method.
    1.60 +// If the Arrays are not similar false is returned.
    1.61 +function exhaustiveSliceTest(testname, a)
    1.62 +{
    1.63 +  var x = 0;
    1.64 +  var y = 0;
    1.65 +  var errorMessage;
    1.66 +  var reason = "";
    1.67 +  var passed = true;
    1.68 +
    1.69 +  for (x = -(2 + a.length); x <= (2 + a.length); x++)
    1.70 +    for (y = (2 + a.length); y >= -(2 + a.length); y--)
    1.71 +    {
    1.72 +      var b  = a.slice(x,y);
    1.73 +      var c = mySlice(a,x,y);
    1.74 +
    1.75 +      if (String(b) != String(c))
    1.76 +      {
    1.77 +	errorMessage =
    1.78 +	  "ERROR: 'TEST FAILED' ERROR: 'TEST FAILED' ERROR: 'TEST FAILED'\n" +
    1.79 +	  "            test: " + "a.slice(" + x + "," + y + ")\n" +
    1.80 +	  "               a: " + String(a) + "\n" +
    1.81 +	  "   actual result: " + String(b) + "\n" +
    1.82 +	  " expected result: " + String(c) + "\n";
    1.83 +	writeHeaderToLog(errorMessage);
    1.84 +	reason = reason + errorMessage;
    1.85 +	passed = false;
    1.86 +      }
    1.87 +    }
    1.88 +  var testCase = new TestCase(SECTION, testname, true, passed);
    1.89 +  if (passed == false)
    1.90 +    testCase.reason = reason;
    1.91 +  return testCase;
    1.92 +}

mercurial