[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Constraints / Tolerance.cs
1 // ***********************************************************************
2 // Copyright (c) 2008 Charlie Poole
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
23 #define PORTABLE
24 #define TIZEN
25 #define NUNIT_FRAMEWORK
26 #define NUNITLITE
27 #define NET_4_5
28 #define PARALLEL
29 using System;
30
31 namespace NUnit.Framework.Constraints
32 {
33     /// <summary>
34     /// The Tolerance class generalizes the notion of a tolerance
35     /// within which an equality test succeeds. Normally, it is
36     /// used with numeric types, but it can be used with any
37     /// type that supports taking a difference between two 
38     /// objects and comparing that difference to a value.
39     /// </summary>
40     public class Tolerance
41     {
42         private readonly ToleranceMode mode;
43         private readonly object amount;
44
45         private const string ModeMustFollowTolerance = "Tolerance amount must be specified before setting mode";
46         private const string MultipleToleranceModes = "Tried to use multiple tolerance modes at the same time";
47         private const string NumericToleranceRequired = "A numeric tolerance is required";
48
49         /// <summary>
50         /// Returns a default Tolerance object, equivalent to 
51         /// specifying an exact match unless <see cref="GlobalSettings.DefaultFloatingPointTolerance"/>
52         /// is set, in which case, the <see cref="GlobalSettings.DefaultFloatingPointTolerance"/>
53         /// will be used.
54         /// </summary>
55         public static Tolerance Default
56         {
57             get { return new Tolerance(0, ToleranceMode.Unset); }
58         }
59
60         /// <summary>
61         /// Returns an empty Tolerance object, equivalent to 
62         /// specifying an exact match even if 
63         /// <see cref="GlobalSettings.DefaultFloatingPointTolerance"/> is set.
64         /// </summary>
65         public static Tolerance Exact
66         {
67             get { return new Tolerance(0, ToleranceMode.Linear); }
68         }
69
70         /// <summary>
71         /// Constructs a linear tolerance of a specified amount
72         /// </summary>
73         public Tolerance(object amount) : this(amount, ToleranceMode.Linear) { }
74
75         /// <summary>
76         /// Constructs a tolerance given an amount and <see cref="ToleranceMode"/>
77         /// </summary>
78         private Tolerance(object amount, ToleranceMode mode)
79         {
80             this.amount = amount;
81             this.mode = mode;
82         }
83
84         /// <summary>
85         /// Gets the <see cref="ToleranceMode"/> for the current Tolerance
86         /// </summary>
87         public ToleranceMode Mode
88         {
89             get { return this.mode; }
90         }
91         
92
93         /// <summary>
94         /// Tests that the current Tolerance is linear with a 
95         /// numeric value, throwing an exception if it is not.
96         /// </summary>
97         private void CheckLinearAndNumeric()
98         {
99             if (mode != ToleranceMode.Linear)
100                 throw new InvalidOperationException(mode == ToleranceMode.Unset
101                     ? ModeMustFollowTolerance
102                     : MultipleToleranceModes);
103
104             if (!Numerics.IsNumericType(amount))
105                 throw new InvalidOperationException(NumericToleranceRequired);
106         }
107
108         /// <summary>
109         /// Gets the value of the current Tolerance instance.
110         /// </summary>
111         public object Value
112         {
113             get { return amount; }
114         }
115
116         /// <summary>
117         /// Returns a new tolerance, using the current amount as a percentage.
118         /// </summary>
119         public Tolerance Percent
120         {
121             get
122             {
123                 CheckLinearAndNumeric();
124                 return new Tolerance(this.amount, ToleranceMode.Percent);
125             }
126         }
127
128         /// <summary>
129         /// Returns a new tolerance, using the current amount in Ulps
130         /// </summary>
131         public Tolerance Ulps
132         {
133             get
134             {
135                 CheckLinearAndNumeric();
136                 return new Tolerance(this.amount, ToleranceMode.Ulps);
137             }
138         }
139
140         /// <summary>
141         /// Returns a new tolerance with a <see cref="TimeSpan"/> as the amount, using 
142         /// the current amount as a number of days.
143         /// </summary>
144         public Tolerance Days
145         {
146             get
147             {
148                 CheckLinearAndNumeric();
149                 return new Tolerance(TimeSpan.FromDays(Convert.ToDouble(amount)));
150             }
151         }
152
153         /// <summary>
154         /// Returns a new tolerance with a <see cref="TimeSpan"/> as the amount, using 
155         /// the current amount as a number of hours.
156         /// </summary>
157         public Tolerance Hours
158         {
159             get
160             {
161                 CheckLinearAndNumeric();
162                 return new Tolerance(TimeSpan.FromHours(Convert.ToDouble(amount)));
163             }
164         }
165
166         /// <summary>
167         /// Returns a new tolerance with a <see cref="TimeSpan"/> as the amount, using 
168         /// the current amount as a number of minutes.
169         /// </summary>
170         public Tolerance Minutes
171         {
172             get
173             {
174                 CheckLinearAndNumeric();
175                 return new Tolerance(TimeSpan.FromMinutes(Convert.ToDouble(amount)));
176             }
177         }
178
179         /// <summary>
180         /// Returns a new tolerance with a <see cref="TimeSpan"/> as the amount, using 
181         /// the current amount as a number of seconds.
182         /// </summary>
183         public Tolerance Seconds
184         {
185             get
186             {
187                 CheckLinearAndNumeric();
188                 return new Tolerance(TimeSpan.FromSeconds(Convert.ToDouble(amount)));
189             }
190         }
191
192         /// <summary>
193         /// Returns a new tolerance with a <see cref="TimeSpan"/> as the amount, using 
194         /// the current amount as a number of milliseconds.
195         /// </summary>
196         public Tolerance Milliseconds
197         {
198             get
199             {
200                 CheckLinearAndNumeric();
201                 return new Tolerance(TimeSpan.FromMilliseconds(Convert.ToDouble(amount)));
202             }
203         }
204
205         /// <summary>
206         /// Returns a new tolerance with a <see cref="TimeSpan"/> as the amount, using 
207         /// the current amount as a number of clock ticks.
208         /// </summary>
209         public Tolerance Ticks
210         {
211             get
212             {
213                 CheckLinearAndNumeric();
214                 return new Tolerance(TimeSpan.FromTicks(Convert.ToInt64(amount)));
215             }
216         }
217
218         /// <summary>
219         /// Returns true if the current tolerance has not been set or is using the .
220         /// </summary>
221         public bool IsUnsetOrDefault
222         {
223             get { return mode == ToleranceMode.Unset; }
224         }
225     }
226 }