[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / System.Drawing / Test / System.Drawing / TestStringFormat.cs
blobbd11fbd30ac366b15712c98186cad93001e17eb5
1 //
2 // StringFormat class testing unit
3 //
4 // Authors:
5 // Jordi Mas i Hernàndez (jordi@ximian.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
7 //
8 // (C) 2004 Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System;
32 using System.ComponentModel;
33 using System.Drawing;
34 using System.Drawing.Text;
35 using System.Security.Permissions;
36 using NUnit.Framework;
38 namespace MonoTests.System.Drawing{
40 [TestFixture]
41 public class StringFormatTest {
43 private void CheckDefaults (StringFormat sf)
45 Assert.AreEqual (StringAlignment.Near, sf.Alignment, "Alignment");
46 Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
47 Assert.AreEqual (StringDigitSubstitute.User, sf.DigitSubstitutionMethod, "DigitSubstitutionMethod");
48 Assert.AreEqual ((StringFormatFlags) 0, sf.FormatFlags, "FormatFlags");
49 Assert.AreEqual (HotkeyPrefix.None, sf.HotkeyPrefix, "HotkeyPrefix");
50 Assert.AreEqual (StringAlignment.Near, sf.LineAlignment, "LineAlignment");
51 Assert.AreEqual (StringTrimming.Character, sf.Trimming, "Trimming");
54 [Test]
55 public void Default ()
57 using (StringFormat sf = new StringFormat ()) {
58 CheckDefaults (sf);
59 Assert.AreEqual ("[StringFormat, FormatFlags=0]", sf.ToString (), "ToString");
60 // check setters validations
61 sf.FormatFlags = (StringFormatFlags) Int32.MinValue;
62 Assert.AreEqual ((StringFormatFlags) Int32.MinValue, sf.FormatFlags, "Min-FormatFlags");
63 Assert.AreEqual ("[StringFormat, FormatFlags=-2147483648]", sf.ToString (), "ToString-2");
67 [Test]
68 public void Default_Dispose ()
70 StringFormat sf = new StringFormat ();
71 sf.Dispose ();
72 Assert.Throws<ArgumentException> (() => sf.ToString ());
75 [Test]
76 public void ctor_StringFormat_Null ()
78 Assert.Throws<ArgumentNullException> (() => new StringFormat (null));
81 [Test]
82 public void ctor_StringFormat ()
84 using (StringFormat sf = new StringFormat (StringFormat.GenericTypographic)) {
85 CheckTypographic (sf);
89 [Test]
90 public void ctor_StringFormatFlags ()
92 using (StringFormat sf = new StringFormat ((StringFormatFlags)Int32.MinValue)) {
93 Assert.AreEqual ((StringFormatFlags) Int32.MinValue, sf.FormatFlags, "FormatFlags");
97 [Test]
98 public void ctor_StringFormatFlags_Int32 ()
100 using (StringFormat sf = new StringFormat ((StringFormatFlags) Int32.MinValue, Int32.MinValue)) {
101 Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
102 Assert.AreEqual ((StringFormatFlags) Int32.MinValue, sf.FormatFlags, "FormatFlags");
106 [Test]
107 public void GenericDefault ()
109 CheckDefaults (StringFormat.GenericDefault);
112 [Test]
113 public void GenericDefault_Dispose ()
115 StringFormat.GenericDefault.Dispose ();
116 CheckDefaults (StringFormat.GenericDefault);
119 [Test]
120 public void GenericDefault_Local_Dispose ()
122 StringFormat sf = StringFormat.GenericDefault;
123 sf.Dispose (); // can't be cached
124 Assert.Throws<ArgumentException> (() => CheckDefaults (sf));
127 private void CheckTypographic (StringFormat sf)
129 Assert.AreEqual (StringAlignment.Near, sf.Alignment, "Alignment");
130 Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
131 Assert.AreEqual (StringDigitSubstitute.User, sf.DigitSubstitutionMethod, "DigitSubstitutionMethod");
132 Assert.AreEqual (StringFormatFlags.FitBlackBox | StringFormatFlags.LineLimit | StringFormatFlags.NoClip, sf.FormatFlags, "FormatFlags");
133 Assert.AreEqual (HotkeyPrefix.None, sf.HotkeyPrefix, "HotkeyPrefix");
134 Assert.AreEqual (StringAlignment.Near, sf.LineAlignment, "LineAlignment");
135 Assert.AreEqual (StringTrimming.None, sf.Trimming, "Trimming");
138 [Test]
139 public void GenericTypographic ()
141 StringFormat sf = StringFormat.GenericTypographic;
142 CheckTypographic (sf);
143 Assert.AreEqual ("[StringFormat, FormatFlags=FitBlackBox, LineLimit, NoClip]", sf.ToString (), "ToString");
146 [Test]
147 public void GenericTypographic_Dispose ()
149 StringFormat.GenericTypographic.Dispose ();
150 CheckTypographic (StringFormat.GenericTypographic);
153 [Test]
154 public void GenericTypographic_Local_Dispose ()
156 StringFormat sf = StringFormat.GenericTypographic;
157 sf.Dispose (); // can't be cached
158 Assert.Throws<ArgumentException> (() => CheckTypographic (sf));
161 [Test]
162 public void Alignment_All ()
164 using (StringFormat sf = new StringFormat ()) {
165 foreach (StringAlignment sa in Enum.GetValues (typeof (StringAlignment))) {
166 sf.Alignment = sa;
167 Assert.AreEqual (sa, sf.Alignment, sa.ToString ());
172 [Test]
173 public void Alignment_Invalid ()
175 using (StringFormat sf = new StringFormat ()) {
176 Assert.Throws<InvalidEnumArgumentException> (() => sf.Alignment = (StringAlignment) Int32.MinValue);
180 [Test]
181 public void HotkeyPrefix_All ()
183 using (StringFormat sf = new StringFormat ()) {
184 foreach (HotkeyPrefix hp in Enum.GetValues (typeof (HotkeyPrefix))) {
185 sf.HotkeyPrefix = hp;
186 Assert.AreEqual (hp, sf.HotkeyPrefix, hp.ToString ());
191 [Test]
192 public void HotkeyPrefix_Invalid ()
194 using (StringFormat sf = new StringFormat ()) {
195 Assert.Throws<InvalidEnumArgumentException> (() => sf.HotkeyPrefix = (HotkeyPrefix) Int32.MinValue);
199 [Test]
200 public void LineAlignment_All ()
202 using (StringFormat sf = new StringFormat ()) {
203 foreach (StringAlignment sa in Enum.GetValues (typeof (StringAlignment))) {
204 sf.LineAlignment = sa;
205 Assert.AreEqual (sa, sf.LineAlignment, sa.ToString ());
210 [Test]
211 public void LineAlignment_Invalid ()
213 using (StringFormat sf = new StringFormat ()) {
214 Assert.Throws<InvalidEnumArgumentException> (() => sf.LineAlignment = (StringAlignment) Int32.MinValue);
218 [Test]
219 public void Trimming_All ()
221 using (StringFormat sf = new StringFormat ()) {
222 foreach (StringTrimming st in Enum.GetValues (typeof (StringTrimming))) {
223 sf.Trimming = st;
224 Assert.AreEqual (st, sf.Trimming, st.ToString ());
229 [Test]
230 public void Trimming_Invalid ()
232 using (StringFormat sf = new StringFormat ()) {
233 Assert.Throws<InvalidEnumArgumentException> (() => sf.Trimming = (StringTrimming) Int32.MinValue);
237 [Test]
238 public void Clone()
240 using (StringFormat sf = new StringFormat ()) {
241 using (StringFormat clone = (StringFormat) sf.Clone ()) {
242 CheckDefaults (clone);
247 [Test]
248 public void Clone_Complex ()
250 using (StringFormat sf = new StringFormat ()) {
251 CharacterRange[] ranges = new CharacterRange [2];
252 ranges[0].First = 1;
253 ranges[0].Length = 2;
254 ranges[1].First = 3;
255 ranges[1].Length = 4;
256 sf.SetMeasurableCharacterRanges (ranges);
258 float[] stops = new float [2];
259 stops [0] = 6.0f;
260 stops [1] = 7.0f;
261 sf.SetTabStops (5.0f, stops);
263 using (StringFormat clone = (StringFormat) sf.Clone ()) {
264 CheckDefaults (clone);
266 float first;
267 float[] cloned_stops = clone.GetTabStops (out first);
268 Assert.AreEqual (5.0f, first, "first");
269 Assert.AreEqual (6.0f, cloned_stops[0], "cloned_stops[0]");
270 Assert.AreEqual (7.0f, cloned_stops[1], "cloned_stops[1]");
275 [Test]
276 public void TestFormatFlags()
278 using (StringFormat smf = new StringFormat ()) {
279 smf.FormatFlags = StringFormatFlags.DisplayFormatControl;
280 Assert.AreEqual (StringFormatFlags.DisplayFormatControl, smf.FormatFlags);
284 [Test]
285 public void TabsStops()
287 using (StringFormat smf = new StringFormat ()) {
288 float firstTabOffset;
289 float[] tabsSrc = { 100, 200, 300, 400 };
290 float[] tabStops;
292 smf.SetTabStops (200, tabsSrc);
293 tabStops = smf.GetTabStops (out firstTabOffset);
295 Assert.AreEqual (200, firstTabOffset);
296 Assert.AreEqual (tabsSrc.Length, tabStops.Length);
297 Assert.AreEqual (tabsSrc[0], tabStops[0]);
298 Assert.AreEqual (tabsSrc[1], tabStops[1]);
299 Assert.AreEqual (tabsSrc[2], tabStops[2]);
300 Assert.AreEqual (tabsSrc[3], tabStops[3]);
304 [Test]
305 public void SetTabStops_Null ()
307 using (StringFormat sf = new StringFormat ()) {
308 Assert.Throws<NullReferenceException> (() => sf.SetTabStops (Single.NaN, null));
312 [Test]
313 public void SetDigitSubstitution ()
315 using (StringFormat sf = new StringFormat ()) {
316 sf.SetDigitSubstitution (Int32.MinValue, (StringDigitSubstitute) Int32.MinValue);
317 Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
318 Assert.AreEqual ((StringDigitSubstitute) Int32.MinValue, sf.DigitSubstitutionMethod, "DigitSubstitutionMethod");
322 [Test]
323 public void SetMeasurableCharacterRanges_Null ()
325 using (StringFormat sf = new StringFormat ()) {
326 Assert.Throws<NullReferenceException> (() => sf.SetMeasurableCharacterRanges (null));
330 [Test]
331 public void SetMeasurableCharacterRanges_Empty ()
333 using (StringFormat sf = new StringFormat ()) {
334 CharacterRange[] range = new CharacterRange[0];
335 sf.SetMeasurableCharacterRanges (range);
339 [Test]
340 public void SetMeasurableCharacterRanges_Max ()
342 using (StringFormat sf = new StringFormat ()) {
343 CharacterRange[] range = new CharacterRange[32];
344 sf.SetMeasurableCharacterRanges (range);
348 [Test]
349 public void SetMeasurableCharacterRanges_TooBig ()
351 using (StringFormat sf = new StringFormat ()) {
352 CharacterRange[] range = new CharacterRange[33];
353 Assert.Throws<OverflowException> (() => sf.SetMeasurableCharacterRanges (range));