1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 #include "UTFStrings.h"
40 #include "nsIServiceManager.h"
41 #include "nsStaticAtom.h"
48 for (unsigned int i
= 0; i
< NS_ARRAY_LENGTH(ValidStrings
); ++i
) {
49 nsDependentString
str16(ValidStrings
[i
].m16
);
50 nsDependentCString
str8(ValidStrings
[i
].m8
);
52 nsCOMPtr
<nsIAtom
> atom
= do_GetAtom(str16
);
54 if (!atom
->Equals(str16
) || !atom
->EqualsUTF8(str8
))
59 atom
->ToString(tmp16
);
60 atom
->ToUTF8String(tmp8
);
61 if (!str16
.Equals(tmp16
) || !str8
.Equals(tmp8
))
64 if (!nsDependentString(atom
->GetUTF16String()).Equals(str16
))
67 if (!nsAtomString(atom
).Equals(str16
) ||
68 !nsDependentAtomString(atom
).Equals(str16
) ||
69 !nsAtomCString(atom
).Equals(str8
))
79 for (unsigned int i
= 0; i
< NS_ARRAY_LENGTH(ValidStrings
); ++i
) {
80 nsCOMPtr
<nsIAtom
> atom16
= do_GetAtom(ValidStrings
[i
].m16
);
81 nsCOMPtr
<nsIAtom
> atom8
= do_GetAtom(ValidStrings
[i
].m8
);
93 unique
.AssignLiteral("this is a unique string !@#$");
95 nsCOMPtr
<nsIAtom
> atom
= do_GetAtom(unique
);
97 return unique
.get() == atom
->GetUTF16String();
103 nsAutoString
str(NS_LITERAL_STRING("string with a \0 char"));
104 nsDependentString
strCut(str
.get());
106 if (str
.Equals(strCut
))
109 nsCOMPtr
<nsIAtom
> atomCut
= do_GetAtom(strCut
);
110 nsCOMPtr
<nsIAtom
> atom
= do_GetAtom(str
);
112 return atom
->GetLength() == str
.Length() &&
114 atom
->EqualsUTF8(NS_ConvertUTF16toUTF8(str
)) &&
116 atomCut
->Equals(strCut
);
122 for (unsigned int i
= 0; i
< NS_ARRAY_LENGTH(Invalid16Strings
); ++i
) {
123 nsrefcnt count
= NS_GetNumberOfAtoms();
126 nsCOMPtr
<nsIAtom
> atom16
= do_GetAtom(Invalid16Strings
[i
].m16
);
127 if (!atom16
->Equals(nsDependentString(Invalid16Strings
[i
].m16
)))
131 if (count
!= NS_GetNumberOfAtoms())
135 for (unsigned int i
= 0; i
< NS_ARRAY_LENGTH(Invalid8Strings
); ++i
) {
136 nsrefcnt count
= NS_GetNumberOfAtoms();
139 nsCOMPtr
<nsIAtom
> atom8
= do_GetAtom(Invalid8Strings
[i
].m8
);
140 nsCOMPtr
<nsIAtom
> atom16
= do_GetAtom(Invalid8Strings
[i
].m16
);
141 if (atom16
!= atom8
||
142 !atom16
->Equals(nsDependentString(Invalid8Strings
[i
].m16
)))
146 if (count
!= NS_GetNumberOfAtoms())
150 // Don't run this test in debug builds as that intentionally asserts.
152 nsCOMPtr
<nsIAtom
> emptyAtom
= do_GetAtom("");
154 for (unsigned int i
= 0; i
< NS_ARRAY_LENGTH(Malformed8Strings
); ++i
) {
155 nsrefcnt count
= NS_GetNumberOfAtoms();
157 nsCOMPtr
<nsIAtom
> atom8
= do_GetAtom(Malformed8Strings
[i
]);
158 if (atom8
!= emptyAtom
||
159 count
!= NS_GetNumberOfAtoms())
167 #define FIRST_ATOM_STR "first static atom. Hello!"
168 #define SECOND_ATOM_STR "second static atom. @World!"
169 #define THIRD_ATOM_STR "third static atom?!"
171 static nsIAtom
* sAtom1
= 0;
172 static nsIAtom
* sAtom2
= 0;
173 static nsIAtom
* sAtom3
= 0;
174 NS_STATIC_ATOM_BUFFER(sAtom1_buffer
, FIRST_ATOM_STR
)
175 NS_STATIC_ATOM_BUFFER(sAtom2_buffer
, SECOND_ATOM_STR
)
176 NS_STATIC_ATOM_BUFFER(sAtom3_buffer
, THIRD_ATOM_STR
)
177 static const nsStaticAtom sAtoms_info
[] = {
178 NS_STATIC_ATOM(sAtom1_buffer
, &sAtom1
),
179 NS_STATIC_ATOM(sAtom2_buffer
, &sAtom2
),
180 NS_STATIC_ATOM(sAtom3_buffer
, &sAtom3
),
184 isStaticAtom(nsIAtom
* atom
)
186 // Don't use logic && in order to ensure that all addrefs/releases are always
187 // run, even if one of the tests fail. This allows us to run this code on a
188 // non-static atom without affecting its refcount.
189 return (atom
->AddRef() == 2) &
190 (atom
->AddRef() == 2) &
191 (atom
->AddRef() == 2) &
192 (atom
->Release() == 1) &
193 (atom
->Release() == 1) &
194 (atom
->Release() == 1);
200 nsrefcnt count
= NS_GetNumberOfAtoms();
202 nsCOMPtr
<nsIAtom
> thirdNonPerm
= do_GetAtom(THIRD_ATOM_STR
);
204 if (isStaticAtom(thirdNonPerm
))
207 if (!thirdNonPerm
|| NS_GetNumberOfAtoms() != count
+ 1)
210 NS_RegisterStaticAtoms(sAtoms_info
, NS_ARRAY_LENGTH(sAtoms_info
));
213 sAtom1
->Equals(NS_LITERAL_STRING(FIRST_ATOM_STR
)) &&
214 isStaticAtom(sAtom1
) &&
216 sAtom2
->Equals(NS_LITERAL_STRING(SECOND_ATOM_STR
)) &&
217 isStaticAtom(sAtom2
) &&
219 sAtom3
->Equals(NS_LITERAL_STRING(THIRD_ATOM_STR
)) &&
220 isStaticAtom(sAtom3
) &&
221 NS_GetNumberOfAtoms() == count
+ 3 &&
222 thirdNonPerm
== sAtom3
;
225 #define FIRST_PERM_ATOM_STR "first permanent atom. Hello!"
226 #define SECOND_PERM_ATOM_STR "second permanent atom. @World!"
231 nsrefcnt count
= NS_GetNumberOfAtoms();
234 nsCOMPtr
<nsIAtom
> first
= do_GetAtom(FIRST_PERM_ATOM_STR
);
235 if (!first
->Equals(NS_LITERAL_STRING(FIRST_PERM_ATOM_STR
)) ||
239 nsCOMPtr
<nsIAtom
> first_p
=
240 NS_NewPermanentAtom(NS_LITERAL_STRING(FIRST_PERM_ATOM_STR
));
241 if (!first_p
->Equals(NS_LITERAL_STRING(FIRST_PERM_ATOM_STR
)) ||
242 !isStaticAtom(first_p
) ||
246 nsCOMPtr
<nsIAtom
> second_p
=
247 NS_NewPermanentAtom(NS_LITERAL_STRING(SECOND_PERM_ATOM_STR
));
248 if (!second_p
->Equals(NS_LITERAL_STRING(SECOND_PERM_ATOM_STR
)) ||
249 !isStaticAtom(second_p
))
252 nsCOMPtr
<nsIAtom
> second
= do_GetAtom(SECOND_PERM_ATOM_STR
);
253 if (!second
->Equals(NS_LITERAL_STRING(SECOND_PERM_ATOM_STR
)) ||
254 !isStaticAtom(second
) ||
259 return NS_GetNumberOfAtoms() == count
+ 2;
262 typedef PRBool (*TestFunc
)();
264 static const struct Test
271 { "test_basic", test_basic
},
272 { "test_16vs8", test_16vs8
},
273 { "test_buffersharing", test_buffersharing
},
274 { "test_null", test_null
},
275 { "test_invalid", test_invalid
},
276 // FIXME: Bug 577500 TestAtoms fails when run in dist/bin due to
277 // static atom table already being closed. TestStaticAtoms has similar
280 { "test_atomtable", test_atomtable
},
281 { "test_permanent", test_permanent
},
288 using namespace TestAtoms
;
293 nsCOMPtr
<nsIServiceManager
> servMan
;
294 NS_InitXPCOM2(getter_AddRefs(servMan
), nsnull
, nsnull
);
296 for (const Test
* t
= tests
; t
->name
!= nsnull
; ++t
)
298 printf("%25s : %s\n", t
->name
, t
->func() ? "SUCCESS" : "FAILURE <--");
302 NS_ShutdownXPCOM(nsnull
);