# Global Metrics

path: .metrics.halstead.N1
old: 266.0
new: 411.0
path: .metrics.halstead.n2
old: 55.0
new: 95.0
path: .metrics.halstead.time
old: 6264.512921024726
new: 11771.425774076864
path: .metrics.halstead.bugs
old: 0.7780101954397247
new: 1.184712928035086
path: .metrics.halstead.estimated_program_length
old: 410.21345012221025
new: 722.2437784014606
path: .metrics.halstead.effort
old: 112761.23257844508
new: 211885.66393338356
path: .metrics.halstead.volume
old: 2924.0300762915977
new: 5228.347551602971
path: .metrics.halstead.N2
old: 202.0
new: 350.0
path: .metrics.halstead.difficulty
old: 38.56363636363636
new: 40.526315789473685
path: .metrics.halstead.level
old: 0.02593116454502593
new: 0.024675324675324673
path: .metrics.halstead.length
old: 468.0
new: 761.0
path: .metrics.halstead.purity_ratio
old: 0.8765244660730989
new: 0.9490719821306972
path: .metrics.halstead.vocabulary
old: 76.0
new: 117.0
path: .metrics.halstead.n1
old: 21.0
new: 22.0
path: .metrics.nargs.average
old: 1.0
new: 1.2222222222222223
path: .metrics.nargs.sum
old: 1.0
new: 11.0
path: .metrics.nom.total
old: 1.0
new: 9.0
path: .metrics.nom.functions
old: 1.0
new: 9.0
path: .metrics.mi.mi_sei
old: 46.05190365218474
new: 0.6360629546908214
path: .metrics.mi.mi_original
old: 52.11255251988824
new: 37.93348361789465
path: .metrics.mi.mi_visual_studio
old: 30.475176912215343
new: 22.183323753154767
path: .metrics.cyclomatic.average
old: 4.0
new: 1.6666666666666667
path: .metrics.cyclomatic.sum
old: 8.0
new: 20.0
path: .metrics.nexits.average
old: 7.0
new: 0.2222222222222222
path: .metrics.nexits.sum
old: 7.0
new: 2.0
path: .metrics.loc.ploc
old: 64.0
new: 130.0
path: .metrics.loc.sloc
old: 106.0
new: 178.0
path: .metrics.loc.lloc
old: 35.0
new: 46.0
path: .metrics.loc.blank
old: 0.0
new: 36.0
path: .metrics.loc.cloc
old: 59.0
new: 12.0
path: .metrics.cognitive.average
old: 12.0
new: 0.8888888888888888
path: .metrics.cognitive.sum
old: 12.0
new: 8.0

# Spaces Data

## Minimal test - lines (21, 178)

path: .spaces[0].metrics.nargs.average
old: 1.0
new: 1.2222222222222223
path: .spaces[0].metrics.nargs.sum
old: 1.0
new: 11.0
path: .spaces[0].metrics.nom.functions
old: 1.0
new: 9.0
path: .spaces[0].metrics.nom.total
old: 1.0
new: 9.0
path: .spaces[0].metrics.nexits.sum
old: 7.0
new: 2.0
path: .spaces[0].metrics.nexits.average
old: 7.0
new: 0.2222222222222222
path: .spaces[0].metrics.mi.mi_visual_studio
old: 39.51506794559723
new: 23.549012074578403
path: .spaces[0].metrics.mi.mi_original
old: 67.57076618697127
new: 40.268810647529065
path: .spaces[0].metrics.mi.mi_sei
old: 54.73159491764865
new: -0.8042661382827294
path: .spaces[0].metrics.cyclomatic.sum
old: 7.0
new: 19.0
path: .spaces[0].metrics.cyclomatic.average
old: 7.0
new: 1.7272727272727273
path: .spaces[0].metrics.halstead.n2
old: 40.0
new: 87.0
path: .spaces[0].metrics.halstead.volume
old: 2419.740833725658
new: 5055.833690608364
path: .spaces[0].metrics.halstead.difficulty
old: 45.4125
new: 43.11494252873563
path: .spaces[0].metrics.halstead.N2
old: 173.0
new: 341.0
path: .spaces[0].metrics.halstead.time
old: 6104.804478420358
new: 12110.109944745833
path: .spaces[0].metrics.halstead.bugs
old: 0.7647302237761769
new: 1.2073294824101672
path: .spaces[0].metrics.halstead.level
old: 0.022020368841178087
new: 0.023193814982671288
path: .spaces[0].metrics.halstead.n1
old: 21.0
new: 22.0
path: .spaces[0].metrics.halstead.vocabulary
old: 61.0
new: 109.0
path: .spaces[0].metrics.halstead.effort
old: 109886.48061156644
new: 217981.97900542495
path: .spaces[0].metrics.halstead.purity_ratio
old: 0.7478328178280599
new: 0.8817183129168138
path: .spaces[0].metrics.halstead.N1
old: 235.0
new: 406.0
path: .spaces[0].metrics.halstead.estimated_program_length
old: 305.11578967384844
new: 658.6435797488599
path: .spaces[0].metrics.halstead.length
old: 408.0
new: 747.0
path: .spaces[0].metrics.loc.sloc
old: 44.0
new: 158.0
path: .spaces[0].metrics.loc.cloc
old: 9.0
new: 6.0
path: .spaces[0].metrics.loc.lloc
old: 35.0
new: 46.0
path: .spaces[0].metrics.loc.ploc
old: 44.0
new: 121.0
path: .spaces[0].metrics.loc.blank
old: 0.0
new: 31.0
path: .spaces[0].metrics.cognitive.sum
old: 12.0
new: 8.0
path: .spaces[0].metrics.cognitive.average
old: 12.0
new: 0.8888888888888888

### Code

    namespace TestAtoms {

    TEST(Atoms, Basic)
    {
      for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) {
        nsDependentString str16(ValidStrings[i].m16);
        nsDependentCString str8(ValidStrings[i].m8);

        RefPtr atom = NS_Atomize(str16);

        EXPECT_TRUE(atom->Equals(str16));

        nsString tmp16;
        nsCString tmp8;
        atom->ToString(tmp16);
        atom->ToUTF8String(tmp8);
        EXPECT_TRUE(str16.Equals(tmp16));
        EXPECT_TRUE(str8.Equals(tmp8));

        EXPECT_TRUE(nsDependentString(atom->GetUTF16String()).Equals(str16));

        EXPECT_TRUE(nsAtomString(atom).Equals(str16));
        EXPECT_TRUE(nsDependentAtomString(atom).Equals(str16));
        EXPECT_TRUE(nsAtomCString(atom).Equals(str8));
      }
    }

    TEST(Atoms, 16vs8)
    {
      for (unsigned int i = 0; i < ArrayLength(ValidStrings); ++i) {
        RefPtr atom16 = NS_Atomize(ValidStrings[i].m16);
        RefPtr atom8 = NS_Atomize(ValidStrings[i].m8);
        EXPECT_EQ(atom16, atom8);
      }
    }

    TEST(Atoms, Null)
    {
      nsAutoString str(u"string with a \0 char"_ns);
      nsDependentString strCut(str.get());

      EXPECT_FALSE(str.Equals(strCut));

      RefPtr atomCut = NS_Atomize(strCut);
      RefPtr atom = NS_Atomize(str);

      EXPECT_EQ(atom->GetLength(), str.Length());
      EXPECT_TRUE(atom->Equals(str));
      EXPECT_NE(atom, atomCut);
      EXPECT_TRUE(atomCut->Equals(strCut));
    }

    TEST(Atoms, Invalid)
    {
      for (unsigned int i = 0; i < ArrayLength(Invalid16Strings); ++i) {
        nsrefcnt count = NS_GetNumberOfAtoms();

        {
          RefPtr atom16 = NS_Atomize(Invalid16Strings[i].m16);
          EXPECT_TRUE(atom16->Equals(nsDependentString(Invalid16Strings[i].m16)));
        }

        EXPECT_EQ(count, NS_GetNumberOfAtoms());
      }
    #ifndef DEBUG
      // Don't run this test in debug builds as that intentionally asserts.
      for (unsigned int i = 0; i < ArrayLength(Invalid8Strings); ++i) {
        nsrefcnt count = NS_GetNumberOfAtoms();

        {
          RefPtr atom8 = NS_Atomize(Invalid8Strings[i].m8);
          RefPtr atom16 = NS_Atomize(Invalid8Strings[i].m16);
          EXPECT_EQ(atom16, atom8);
          EXPECT_TRUE(atom16->Equals(nsDependentString(Invalid8Strings[i].m16)));
        }

        EXPECT_EQ(count, NS_GetNumberOfAtoms());
      }

      for (unsigned int i = 0; i < ArrayLength(Malformed8Strings); ++i) {
        nsrefcnt count = NS_GetNumberOfAtoms();

        {
          RefPtr atom8 = NS_Atomize(Malformed8Strings[i].m8);
          RefPtr atom16 = NS_Atomize(Malformed8Strings[i].m16);
          EXPECT_EQ(atom8, atom16);
        }

        EXPECT_EQ(count, NS_GetNumberOfAtoms());
      }
    #endif
    }

    #define FIRST_ATOM_STR "first static atom. Hello!"
    #define SECOND_ATOM_STR "second static atom. @World!"
    #define THIRD_ATOM_STR "third static atom?!"

    static bool isStaticAtom(nsAtom* atom) {
      // Don't use logic && in order to ensure that all addrefs/releases are always
      // run, even if one of the tests fail. This allows us to run this code on a
      // non-static atom without affecting its refcount.
      bool rv = (atom->AddRef() == 2);
      rv &= (atom->AddRef() == 2);
      rv &= (atom->AddRef() == 2);

      rv &= (atom->Release() == 1);
      rv &= (atom->Release() == 1);
      rv &= (atom->Release() == 1);
      return rv;
    }

    TEST(Atoms, Table)
    {
      nsrefcnt count = NS_GetNumberOfAtoms();

      RefPtr thirdDynamic = NS_Atomize(THIRD_ATOM_STR);

      EXPECT_FALSE(isStaticAtom(thirdDynamic));

      EXPECT_TRUE(thirdDynamic);
      EXPECT_EQ(NS_GetNumberOfAtoms(), count + 1);
    }

    class nsAtomRunner final : public Runnable {
     public:
      NS_IMETHOD Run() final {
        for (int i = 0; i < 10000; i++) {
          RefPtr atom = NS_Atomize(u"A Testing Atom");
        }
        return NS_OK;
      }

      nsAtomRunner() : Runnable("nsAtomRunner") {}

     private:
      ~nsAtomRunner() = default;
    };

    TEST(Atoms, ConcurrentAccessing)
    {
      static const size_t kThreadCount = 4;
      // Force a GC before so that we don't have any unused atom.
      NS_GetNumberOfAtoms();
      EXPECT_EQ(NS_GetUnusedAtomCount(), int32_t(0));
      nsCOMPtr threads[kThreadCount];
      for (size_t i = 0; i < kThreadCount; i++) {
        nsresult rv = NS_NewNamedThread("Atom Test", getter_AddRefs(threads[i]),
                                        new nsAtomRunner);
        EXPECT_TRUE(NS_SUCCEEDED(rv));
      }
      for (size_t i = 0; i < kThreadCount; i++) {
        threads[i]->Shutdown();
      }
      // We should have one unused atom from this test.
      EXPECT_EQ(NS_GetUnusedAtomCount(), int32_t(1));
    }

    }  // namespace TestAtoms