Add nthroot function
[scilab.git] / scilab / modules / elementary_functions / tests / unit_tests / nthroot.tst
1 // =============================================================================
2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) 2012 - DIGITEO - Adeline CARNIS
4 //
5 //  This file is distributed under the same license as the Scilab package.
6 // =============================================================================
7
8 // <-- JVM NOT MANDATORY -->
9
10 // unit tests for nthroot() function 
11 // =============================================================================
12
13 // 1. Interface
14 // ============
15 assert_checkfalse(execstr("nthroot()"   ,"errcatch") == 0);
16 lasterror(execstr("10000","errcatch") == 0)
17 assert_checkfalse(execstr("nthroot(1)","errcatch") == 0);
18 lasterror(execstr("10000","errcatch") == 0)
19 assert_checkfalse(execstr("nthroot(%i,1)","errcatch") == 0);
20 lasterror(execstr("10000","errcatch") == 0)
21 assert_checkfalse(execstr("nthroot(%t,1)","errcatch") == 0);
22 lasterror(execstr("10000","errcatch") == 0)
23 assert_checkfalse(execstr("nthroot(3,[])","errcatch") == 0);
24 lasterror(execstr("10000","errcatch") == 0)
25 assert_checkfalse(execstr("nthroot(-2,%nan)","errcatch") == 0);
26 lasterror(execstr("10000","errcatch") == 0)
27 assert_checkfalse(execstr("nthroot(-2,%eps)","errcatch") == 0);
28 lasterror(execstr("10000","errcatch") == 0)
29 assert_checkfalse(execstr("nthroot(-3,6)","errcatch") == 0);
30 lasterror(execstr("10000","errcatch") == 0)
31 assert_checkfalse(execstr("nthroot(-3,0)","errcatch") == 0);
32 lasterror(execstr("10000","errcatch") == 0)
33 assert_checkfalse(execstr("nthroot(-%inf,-2)","errcatch") == 0);
34 lasterror(execstr("10000","errcatch") == 0)
35 assert_checkfalse(execstr("nthroot(-2,-2)","errcatch") == 0);
36 lasterror(execstr("10000","errcatch") == 0)
37 assert_checkfalse(execstr("nthroot(-2,2)","errcatch") == 0);
38 lasterror(execstr("10000","errcatch") == 0)
39
40
41 // 2. Singular Values
42 // ==================
43 assert_checkalmostequal(nthroot(2,-2),0.7071068, 1.e-5);
44 assert_checkalmostequal(nthroot(2,2),1.4142136, 1.e-5);
45 assert_checkalmostequal(nthroot(%eps,3),0.0000061, 1.e-2);
46 assert_checkalmostequal(nthroot([1 2],2),[1 1.4142136], 1.e-5);
47 assert_checkequal(nthroot(-2,%inf),-1);
48 assert_checkequal(nthroot(3,%inf),1);
49 assert_checkequal(nthroot(3,-%inf),1);
50 assert_checkequal(nthroot(-3,%inf),-1);
51 assert_checkequal(nthroot(3,%nan),%nan);
52 assert_checkequal(nthroot(3,-%nan),%nan);
53 assert_checkequal(nthroot(3,%eps),%inf);
54 assert_checkequal(nthroot(3,-%eps),0);
55 assert_checkequal(nthroot(3,0),%inf);
56 assert_checkequal(nthroot(1,0),%nan);
57
58 assert_checkequal(nthroot([1 2],%inf),[1 1]);
59 assert_checkequal(nthroot([1 2],%nan),[%nan %nan]);
60 assert_checkequal(nthroot([1 2],%eps),[1 %inf]);
61 assert_checkequal(nthroot([1 2],0),[%nan %inf]);
62
63 assert_checkequal(nthroot(0,0),0);
64 assert_checkequal(nthroot(0,7),0);
65 assert_checkequal(nthroot(0,-7),%inf);
66 assert_checkequal(nthroot(0,%inf),1);
67 assert_checkequal(nthroot(0,-%inf),1);
68 assert_checkequal(nthroot(0,%nan),%nan);
69 assert_checkequal(nthroot(0,%eps),0);
70 assert_checkequal(nthroot(0,-%eps),%inf);
71
72 assert_checkequal(nthroot([0;-1],%inf),[1;-1]);
73 assert_checkequal(nthroot([0;1],0),[0;%nan]);
74
75 // 3. Empty Matrix
76 //==================
77 assert_checkequal(nthroot([],-2),[]);
78 assert_checkequal(nthroot([],3),[]);
79 assert_checkequal(nthroot([],%nan),[]);
80 assert_checkequal(nthroot([],%inf),[]);
81 assert_checkequal(nthroot([],-%inf),[]);
82 assert_checkequal(nthroot([],%eps),[]);
83 assert_checkequal(nthroot([],-%eps),[]);
84 assert_checkequal(nthroot([],0),[]);
85
86 // 4. Limit Values
87 // ================
88 assert_checkequal(nthroot(-%inf,-7),0);
89 assert_checkequal(nthroot(-%inf,7),-%inf);
90 assert_checkequal(nthroot(%inf,-7),0);
91 assert_checkequal(nthroot(%inf,7),%inf);
92 assert_checkequal(nthroot(%inf,%inf),1);
93 assert_checkequal(nthroot(%inf,-%inf),1);
94 assert_checkequal(nthroot(-%inf,%inf),-1);
95 assert_checkequal(nthroot(-%inf,-%inf),-1);
96 assert_checkequal(nthroot(%inf,%inf),1);
97 assert_checkequal(nthroot(%inf,%nan),%nan);
98 assert_checkequal(nthroot(%inf,%eps),%inf);
99 assert_checkequal(nthroot(%inf,-%eps),0);
100 assert_checkequal(nthroot(%inf,0),%inf);
101
102 assert_checkequal(nthroot([%inf;%nan],3),[%inf;%nan]);
103 assert_checkequal(nthroot([%inf;%nan],%nan),[%nan;%nan]);
104 assert_checkequal(nthroot([%inf;%nan],0),[%inf;%nan]);
105
106 assert_checkequal(nthroot([%inf;%eps],%eps),[%inf;0]);
107 assert_checkequal(nthroot([%inf;%eps],%nan),[%nan;%nan]);
108 assert_checkequal(nthroot([%inf;1],0),[%inf;%nan]);
109
110 // 5. Not A Number
111 // =================
112 assert_checkequal(nthroot(%nan,-2),%nan);
113 assert_checkequal(nthroot(%nan,%inf),%nan);
114 assert_checkequal(nthroot(%nan,-%inf),%nan);
115 assert_checkequal(nthroot(%nan,-%eps),%nan);
116 assert_checkequal(nthroot(%nan,0),%nan);
117
118 // 6. X = %eps
119 // ===========
120 assert_checkequal(nthroot(%eps,%inf),1);
121 assert_checkequal(nthroot(%eps,-%inf),1);
122 assert_checkequal(nthroot(-%eps,%inf),-1);
123 assert_checkequal(nthroot(-%eps,-%inf),-1);
124 assert_checkequal(nthroot(%eps,%inf),1);
125 assert_checkequal(nthroot(%eps,%nan),%nan);
126 assert_checkequal(nthroot(%eps,%eps),0);
127 assert_checkequal(nthroot(%eps,-%eps),%inf);
128 assert_checkequal(nthroot(%eps,0),0);