1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include "precompiled.hpp"
  25 #include "runtime/interfaceSupport.hpp"
  26 #include "classfile/symbolTable.hpp"
  27 #include "unittest.hpp"
  28 
  29 TEST_VM(SymbolTable, temp_new_symbol) {
  30   // Assert messages assume these symbols are unique, and the refcounts start at
  31   // one, but code does not rely on this.
  32   JavaThread* THREAD = JavaThread::current();
  33   // the thread should be in vm to use locks
  34   ThreadInVMfromNative ThreadInVMfromNative(THREAD);
  35 
  36   Symbol* abc = SymbolTable::new_symbol("abc", CATCH);
  37   int abccount = abc->refcount();
  38   TempNewSymbol ss = abc;
  39   ASSERT_EQ(ss->refcount(), abccount) << "only one abc";
  40   ASSERT_EQ(ss->refcount(), abc->refcount()) << "should match TempNewSymbol";
  41 
  42   Symbol* efg = SymbolTable::new_symbol("efg", CATCH);
  43   Symbol* hij = SymbolTable::new_symbol("hij", CATCH);
  44   int efgcount = efg->refcount();
  45   int hijcount = hij->refcount();
  46 
  47   TempNewSymbol s1 = efg;
  48   TempNewSymbol s2 = hij;
  49   ASSERT_EQ(s1->refcount(), efgcount) << "one efg";
  50   ASSERT_EQ(s2->refcount(), hijcount) << "one hij";
  51 
  52   // Assignment operator
  53   s1 = s2;
  54   ASSERT_EQ(hij->refcount(), hijcount + 1) << "should be two hij";
  55   ASSERT_EQ(efg->refcount(), efgcount - 1) << "should be no efg";
  56 
  57   s1 = ss; // s1 is abc
  58   ASSERT_EQ(s1->refcount(), abccount + 1) << "should be two abc (s1 and ss)";
  59   ASSERT_EQ(hij->refcount(), hijcount) << "should only have one hij now (s2)";
  60 
  61   s1 = s1; // self assignment
  62   ASSERT_EQ(s1->refcount(), abccount + 1) << "should still be two abc (s1 and ss)";
  63 
  64   TempNewSymbol s3;
  65   Symbol* klm = SymbolTable::new_symbol("klm", CATCH);
  66   int klmcount = klm->refcount();
  67   s3 = klm; // assignment
  68   ASSERT_EQ(s3->refcount(), klmcount) << "only one klm now";
  69 
  70   Symbol* xyz = SymbolTable::new_symbol("xyz", CATCH);
  71   int xyzcount = xyz->refcount();
  72   { // inner scope
  73     TempNewSymbol s_inner = xyz;
  74   }
  75   ASSERT_EQ(xyz->refcount(), xyzcount - 1)
  76           << "Should have been decremented by dtor in inner scope";
  77 }