LCOV - code coverage report
Current view: top level - test/unittests - strings-storage-unittest.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 58 59 98.3 %
Date: 2019-04-17 Functions: 14 21 66.7 %

          Line data    Source code
       1             : // Copyright 2018 the V8 project authors. All rights reserved.
       2             : // Use of this source code is governed by a BSD-style license that can be
       3             : // found in the LICENSE file.
       4             : 
       5             : #include "src/profiler/strings-storage.h"
       6             : 
       7             : #include <cstdio>
       8             : 
       9             : #include "test/unittests/test-utils.h"
      10             : #include "testing/gtest/include/gtest/gtest.h"
      11             : 
      12             : namespace v8 {
      13             : namespace internal {
      14             : 
      15             : typedef TestWithIsolate StringsStorageWithIsolate;
      16             : 
      17           0 : bool StringEq(const char* left, const char* right) {
      18           9 :   return strcmp(left, right) == 0;
      19             : }
      20             : 
      21       15444 : TEST_F(StringsStorageWithIsolate, GetNameFromString) {
      22           2 :   StringsStorage storage;
      23             : 
      24             :   // One char strings are canonical on the v8 heap so use a 2 char string here.
      25           1 :   Handle<String> str = isolate()->factory()->NewStringFromAsciiChecked("xy");
      26           1 :   const char* stored_str = storage.GetName(*str);
      27           1 :   CHECK(StringEq("xy", stored_str));
      28             : 
      29             :   // The storage should de-duplicate the underlying char arrays and return the
      30             :   // exact same pointer for equivalent input strings.
      31           1 :   const char* stored_str_twice = storage.GetName(*str);
      32           1 :   CHECK_EQ(stored_str, stored_str_twice);
      33             : 
      34             :   // Even if the input string was a different one on the v8 heap, if the char
      35             :   // array is the same, it should be de-duplicated.
      36           1 :   Handle<String> str2 = isolate()->factory()->NewStringFromAsciiChecked("xy");
      37           1 :   CHECK_NE(*str, *str2);
      38           1 :   const char* stored_str_thrice = storage.GetName(*str2);
      39           1 :   CHECK_EQ(stored_str_twice, stored_str_thrice);
      40           1 : }
      41             : 
      42       15444 : TEST_F(StringsStorageWithIsolate, GetNameFromSymbol) {
      43           2 :   StringsStorage storage;
      44             : 
      45           1 :   Handle<Symbol> symbol = isolate()->factory()->NewSymbol();
      46           1 :   const char* stored_symbol = storage.GetName(*symbol);
      47           1 :   CHECK(StringEq("<symbol>", stored_symbol));
      48             : 
      49           1 :   Handle<Symbol> symbol2 = isolate()->factory()->NewSymbol();
      50           1 :   CHECK_NE(*symbol, *symbol2);
      51           1 :   const char* stored_symbol2 = storage.GetName(*symbol2);
      52           1 :   CHECK_EQ(stored_symbol, stored_symbol2);
      53           1 : }
      54             : 
      55       15444 : TEST_F(StringsStorageWithIsolate, GetConsName) {
      56           2 :   StringsStorage storage;
      57             : 
      58           1 :   Handle<String> str = isolate()->factory()->NewStringFromAsciiChecked("xy");
      59             : 
      60           1 :   const char* empty_prefix_str = storage.GetConsName("", *str);
      61           1 :   CHECK(StringEq("xy", empty_prefix_str));
      62             : 
      63           1 :   const char* get_str = storage.GetConsName("get ", *str);
      64           1 :   CHECK(StringEq("get xy", get_str));
      65           1 : }
      66             : 
      67       15444 : TEST_F(StringsStorageWithIsolate, GetNameFromInt) {
      68           2 :   StringsStorage storage;
      69             : 
      70           1 :   const char* stored_str = storage.GetName(0);
      71           1 :   CHECK(StringEq("0", stored_str));
      72             : 
      73           1 :   stored_str = storage.GetName(2147483647);
      74           1 :   CHECK(StringEq("2147483647", stored_str));
      75             : 
      76           1 :   stored_str = storage.GetName(std::numeric_limits<int>::min());
      77             :   char str_negative_int[12];
      78             :   snprintf(str_negative_int, sizeof(str_negative_int), "%d",
      79             :            std::numeric_limits<int>::min());
      80           1 :   CHECK(StringEq(str_negative_int, stored_str));
      81           1 : }
      82             : 
      83       15444 : TEST_F(StringsStorageWithIsolate, Format) {
      84           2 :   StringsStorage storage;
      85             : 
      86             :   const char* xy = "xy";
      87           1 :   const char* stored_str = storage.GetFormatted("%s", xy);
      88           1 :   CHECK(StringEq("xy", stored_str));
      89             :   // Check that the string is copied.
      90           1 :   CHECK_NE(xy, stored_str);
      91             : 
      92           1 :   const char* formatted_str = storage.GetFormatted("%s / %s", xy, xy);
      93           1 :   CHECK(StringEq("xy / xy", formatted_str));
      94             : 
      95             :   // A different format specifier that results in the same string should share
      96             :   // the string in storage.
      97           1 :   const char* formatted_str2 = storage.GetFormatted("%s", "xy / xy");
      98           1 :   CHECK_EQ(formatted_str, formatted_str2);
      99           1 : }
     100             : 
     101       15444 : TEST_F(StringsStorageWithIsolate, FormatAndGetShareStorage) {
     102           2 :   StringsStorage storage;
     103             : 
     104           1 :   Handle<String> str = isolate()->factory()->NewStringFromAsciiChecked("xy");
     105           1 :   const char* stored_str = storage.GetName(*str);
     106             : 
     107           1 :   const char* formatted_str = storage.GetFormatted("%s", "xy");
     108           1 :   CHECK_EQ(stored_str, formatted_str);
     109           1 : }
     110             : 
     111             : }  // namespace internal
     112        9264 : }  // namespace v8

Generated by: LCOV version 1.10