• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #include <stdlib.h>
29 
30 #include "v8.h"
31 
32 #include "platform.h"
33 #include "cctest.h"
34 
35 using namespace v8::internal;
36 
37 
TEST(Utils1)38 TEST(Utils1) {
39   CHECK_EQ(-1000000, FastD2I(-1000000.0));
40   CHECK_EQ(-1, FastD2I(-1.0));
41   CHECK_EQ(0, FastD2I(0.0));
42   CHECK_EQ(1, FastD2I(1.0));
43   CHECK_EQ(1000000, FastD2I(1000000.0));
44 
45   CHECK_EQ(-1000000, FastD2I(-1000000.123));
46   CHECK_EQ(-1, FastD2I(-1.234));
47   CHECK_EQ(0, FastD2I(0.345));
48   CHECK_EQ(1, FastD2I(1.234));
49   CHECK_EQ(1000000, FastD2I(1000000.123));
50   // Check that >> is implemented as arithmetic shift right.
51   // If this is not true, then ArithmeticShiftRight() must be changed,
52   // There are also documented right shifts in assembler.cc of
53   // int8_t and intptr_t signed integers.
54   CHECK_EQ(-2, -8 >> 2);
55   CHECK_EQ(-2, static_cast<int8_t>(-8) >> 2);
56   CHECK_EQ(-2, static_cast<int>(static_cast<intptr_t>(-8) >> 2));
57 }
58 
59 
TEST(SNPrintF)60 TEST(SNPrintF) {
61   // Make sure that strings that are truncated because of too small
62   // buffers are zero-terminated anyway.
63   const char* s = "the quick lazy .... oh forget it!";
64   int length = StrLength(s);
65   for (int i = 1; i < length * 2; i++) {
66     static const char kMarker = static_cast<char>(42);
67     Vector<char> buffer = Vector<char>::New(i + 1);
68     buffer[i] = kMarker;
69     int n = OS::SNPrintF(Vector<char>(buffer.start(), i), "%s", s);
70     CHECK(n <= i);
71     CHECK(n == length || n == -1);
72     CHECK_EQ(0, strncmp(buffer.start(), s, i - 1));
73     CHECK_EQ(kMarker, buffer[i]);
74     if (i <= length) {
75       CHECK_EQ(i - 1, StrLength(buffer.start()));
76     } else {
77       CHECK_EQ(length, StrLength(buffer.start()));
78     }
79     buffer.Dispose();
80   }
81 }
82