diff options
Diffstat (limited to 'main/tests/test_math.cpp')
| -rw-r--r-- | main/tests/test_math.cpp | 461 |
1 files changed, 212 insertions, 249 deletions
diff --git a/main/tests/test_math.cpp b/main/tests/test_math.cpp index 43164bb2e..c82ae1abf 100644 --- a/main/tests/test_math.cpp +++ b/main/tests/test_math.cpp @@ -28,26 +28,25 @@ /*************************************************************************/ #include "test_math.h" -#include "ustring.h" -#include "print_string.h" -#include "transform.h" -#include "matrix3.h" -#include "math_funcs.h" #include "camera_matrix.h" -#include "scene/main/node.h" -#include "variant.h" -#include "servers/visual/shader_language.h" +#include "math_funcs.h" +#include "matrix3.h" +#include "os/file_access.h" #include "os/keyboard.h" +#include "os/os.h" +#include "print_string.h" +#include "scene/main/node.h" #include "scene/resources/texture.h" +#include "servers/visual/shader_language.h" +#include "transform.h" +#include "ustring.h" +#include "variant.h" #include "vmap.h" -#include "os/os.h" -#include "os/file_access.h" #include "method_ptrcall.h" namespace TestMath { - class GetClassAndNamespace { String code; @@ -75,11 +74,10 @@ class GetClassAndNamespace { TK_ERROR }; - Token get_token() { while (true) { - switch(code[idx]) { + switch (code[idx]) { case '\n': { @@ -128,28 +126,27 @@ class GetClassAndNamespace { }; case '#': { //compiler directive - while(code[idx]!='\n' && code[idx]!=0) { + while (code[idx] != '\n' && code[idx] != 0) { idx++; } continue; } break; case '/': { - - switch(code[idx+1]) { + switch (code[idx + 1]) { case '*': { // block comment - idx+=2; - while(true) { - if (code[idx]==0) { - error_str="Unterminated comment"; - error=true; + idx += 2; + while (true) { + if (code[idx] == 0) { + error_str = "Unterminated comment"; + error = true; return TK_ERROR; - } else if (code[idx]=='*' &&code[idx+1]=='/') { + } else if (code[idx] == '*' && code[idx + 1] == '/') { - idx+=2; + idx += 2; break; - } else if (code[idx]=='\n') { + } else if (code[idx] == '\n') { line++; } @@ -159,17 +156,16 @@ class GetClassAndNamespace { } break; case '/': { // line comment skip - while(code[idx]!='\n' && code[idx]!=0) { + while (code[idx] != '\n' && code[idx] != 0) { idx++; } } break; default: { - value="/"; + value = "/"; idx++; return TK_SYMBOL; } - } continue; // a comment @@ -179,34 +175,36 @@ class GetClassAndNamespace { CharType begin_str = code[idx]; idx++; - String tk_string=String(); - while(true) { - if (code[idx]==0) { - error_str="Unterminated String"; - error=true; + String tk_string = String(); + while (true) { + if (code[idx] == 0) { + error_str = "Unterminated String"; + error = true; return TK_ERROR; - } else if (code[idx]==begin_str) { + } else if (code[idx] == begin_str) { idx++; break; - } else if (code[idx]=='\\') { + } else if (code[idx] == '\\') { //escaped characters... idx++; CharType next = code[idx]; - if (next==0) { - error_str="Unterminated String"; - error=true; + if (next == 0) { + error_str = "Unterminated String"; + error = true; return TK_ERROR; } - CharType res=0; + CharType res = 0; - switch(next) { + switch (next) { - case 'b': res=8; break; - case 't': res=9; break; - case 'n': res=10; break; - case 'f': res=12; break; - case 'r': res=13; break; - /* too much, not needed for now + case 'b': res = 8; break; + case 't': res = 9; break; + case 'n': res = 10; break; + case 'f': res = 12; break; + case 'r': + res = 13; + break; + /* too much, not needed for now case 'u': { //hexnumbarh - oct is deprecated @@ -245,8 +243,10 @@ class GetClassAndNamespace { } break;*/ - case '\"': res='\"'; break; - case '\\': res='\\'; break; + case '\"': res = '\"'; break; + case '\\': + res = '\\'; + break; //case '/': res='/'; break; default: { res = next; @@ -255,133 +255,130 @@ class GetClassAndNamespace { } break; } - tk_string+=res; + tk_string += res; } else { - if (code[idx]=='\n') + if (code[idx] == '\n') line++; - tk_string+=code[idx]; + tk_string += code[idx]; } idx++; } - value=tk_string; + value = tk_string; return TK_STRING; } break; default: { - if (code[idx]<=32) { + if (code[idx] <= 32) { idx++; break; } - if ( (code[idx]>=33 && code[idx]<=47) || (code[idx]>=58 && code[idx]<=64) || (code[idx]>=91 && code[idx]<=96) || (code[idx]>=123 && code[idx]<=127)){ - value=String::chr(code[idx]); + if ((code[idx] >= 33 && code[idx] <= 47) || (code[idx] >= 58 && code[idx] <= 64) || (code[idx] >= 91 && code[idx] <= 96) || (code[idx] >= 123 && code[idx] <= 127)) { + value = String::chr(code[idx]); idx++; return TK_SYMBOL; } - if (code[idx]=='-' || (code[idx]>='0' && code[idx]<='9')) { + if (code[idx] == '-' || (code[idx] >= '0' && code[idx] <= '9')) { //a number const CharType *rptr; - double number = String::to_double(&code[idx],&rptr); - idx+=(rptr - &code[idx]); - value=number; + double number = String::to_double(&code[idx], &rptr); + idx += (rptr - &code[idx]); + value = number; return TK_NUMBER; - } else if ((code[idx]>='A' && code[idx]<='Z') || (code[idx]>='a' && code[idx]<='z') || code[idx]>127) { + } else if ((code[idx] >= 'A' && code[idx] <= 'Z') || (code[idx] >= 'a' && code[idx] <= 'z') || code[idx] > 127) { String id; - while((code[idx]>='A' && code[idx]<='Z') || (code[idx]>='a' && code[idx]<='z') || code[idx]>127) { + while ((code[idx] >= 'A' && code[idx] <= 'Z') || (code[idx] >= 'a' && code[idx] <= 'z') || code[idx] > 127) { - id+=code[idx]; + id += code[idx]; idx++; } - value=id; + value = id; return TK_IDENTIFIER; } else { - error_str="Unexpected character."; - error=true; + error_str = "Unexpected character."; + error = true; return TK_ERROR; } } - } } } public: - Error parse(const String& p_code,const String& p_known_class_name=String()) { + Error parse(const String &p_code, const String &p_known_class_name = String()) { - code=p_code; - idx=0; - line=0; - error_str=String(); - error=false; - value=Variant(); - class_name=String(); + code = p_code; + idx = 0; + line = 0; + error_str = String(); + error = false; + value = Variant(); + class_name = String(); - bool use_next_class=false; + bool use_next_class = false; Token tk = get_token(); - Map<int,String> namespace_stack; - int curly_stack=0; - + Map<int, String> namespace_stack; + int curly_stack = 0; - while(!error || tk!=TK_EOF) { + while (!error || tk != TK_EOF) { - if (tk==TK_BRACKET_OPEN) { + if (tk == TK_BRACKET_OPEN) { tk = get_token(); - if (tk==TK_IDENTIFIER && String(value)=="ScriptClass") { - if (get_token()==TK_BRACKET_CLOSE) { - use_next_class=true; + if (tk == TK_IDENTIFIER && String(value) == "ScriptClass") { + if (get_token() == TK_BRACKET_CLOSE) { + use_next_class = true; } } - } else if (tk==TK_IDENTIFIER && String(value)=="class") { + } else if (tk == TK_IDENTIFIER && String(value) == "class") { tk = get_token(); - if (tk==TK_IDENTIFIER) { + if (tk == TK_IDENTIFIER) { String name = value; - if (use_next_class || p_known_class_name==name) { - for (Map<int,String>::Element *E=namespace_stack.front();E;E=E->next()) { - class_name+=E->get()+"."; + if (use_next_class || p_known_class_name == name) { + for (Map<int, String>::Element *E = namespace_stack.front(); E; E = E->next()) { + class_name += E->get() + "."; } - class_name+=String(value); + class_name += String(value); break; } } - } else if (tk==TK_IDENTIFIER && String(value)=="namespace") { + } else if (tk == TK_IDENTIFIER && String(value) == "namespace") { String name; int at_level = curly_stack; - while(true) { + while (true) { tk = get_token(); - if (tk==TK_IDENTIFIER) { - name+=String(value); + if (tk == TK_IDENTIFIER) { + name += String(value); } tk = get_token(); - if (tk==TK_PERIOD) { - name+="."; - } else if (tk==TK_CURLY_BRACKET_OPEN) { + if (tk == TK_PERIOD) { + name += "."; + } else if (tk == TK_CURLY_BRACKET_OPEN) { curly_stack++; break; } else { break; //whathever else } - } - if (name!=String()) { - namespace_stack[at_level]=name; + if (name != String()) { + namespace_stack[at_level] = name; } - } else if (tk==TK_CURLY_BRACKET_OPEN) { + } else if (tk == TK_CURLY_BRACKET_OPEN) { curly_stack++; - } else if (tk==TK_CURLY_BRACKET_CLOSE) { + } else if (tk == TK_CURLY_BRACKET_CLOSE) { curly_stack--; if (namespace_stack.has(curly_stack)) { namespace_stack.erase(curly_stack); @@ -394,10 +391,7 @@ public: if (error) return ERR_PARSE_ERROR; - - return OK; - } String get_error() { @@ -407,23 +401,19 @@ public: String get_class() { return class_name; } - }; - void test_vec(Plane p_vec) { - CameraMatrix cm; - cm.set_perspective(45,1,0,100); - Plane v0=cm.xform4(p_vec); + cm.set_perspective(45, 1, 0, 100); + Plane v0 = cm.xform4(p_vec); - print_line("out: "+v0); - v0.normal.z = (v0.d/100.0 *2.0-1.0) * v0.d; - print_line("out_F: "+v0); + print_line("out: " + v0); + v0.normal.z = (v0.d / 100.0 * 2.0 - 1.0) * v0.d; + print_line("out_F: " + v0); - -/*v0: 0, 0, -0.1, 0.1 + /*v0: 0, 0, -0.1, 0.1 v1: 0, 0, 0, 0.1 fix: 0, 0, 0, 0.1 v0: 0, 0, 1.302803, 1.5 @@ -439,49 +429,41 @@ v0: 0, 0, 100, 100 v1: 0, 0, 100, 100 fix: 0, 0, 100, 100 */ - - } -uint32_t ihash( uint32_t a) -{ - a = (a+0x7ed55d16) + (a<<12); - a = (a^0xc761c23c) ^ (a>>19); - a = (a+0x165667b1) + (a<<5); - a = (a+0xd3a2646c) ^ (a<<9); - a = (a+0xfd7046c5) + (a<<3); - a = (a^0xb55a4f09) ^ (a>>16); - return a; +uint32_t ihash(uint32_t a) { + a = (a + 0x7ed55d16) + (a << 12); + a = (a ^ 0xc761c23c) ^ (a >> 19); + a = (a + 0x165667b1) + (a << 5); + a = (a + 0xd3a2646c) ^ (a << 9); + a = (a + 0xfd7046c5) + (a << 3); + a = (a ^ 0xb55a4f09) ^ (a >> 16); + return a; } -uint32_t ihash2( uint32_t a) { - a = (a ^ 61) ^ (a >> 16); - a = a + (a << 3); - a = a ^ (a >> 4); - a = a * 0x27d4eb2d; - a = a ^ (a >> 15); - return a; +uint32_t ihash2(uint32_t a) { + a = (a ^ 61) ^ (a >> 16); + a = a + (a << 3); + a = a ^ (a >> 4); + a = a * 0x27d4eb2d; + a = a ^ (a >> 15); + return a; } -uint32_t ihash3( uint32_t a) -{ - a = (a+0x479ab41d) + (a<<8); - a = (a^0xe4aa10ce) ^ (a>>5); - a = (a+0x9942f0a6) - (a<<14); - a = (a^0x5aedd67d) ^ (a>>3); - a = (a+0x17bea992) + (a<<7); - return a; +uint32_t ihash3(uint32_t a) { + a = (a + 0x479ab41d) + (a << 8); + a = (a ^ 0xe4aa10ce) ^ (a >> 5); + a = (a + 0x9942f0a6) - (a << 14); + a = (a ^ 0x5aedd67d) ^ (a >> 3); + a = (a + 0x17bea992) + (a << 7); + return a; } +MainLoop *test() { - - -MainLoop* test() { - - - print_line("Dvectors: "+itos(MemoryPool::allocs_used)); - print_line("Mem used: "+itos(MemoryPool::total_memory)); - print_line("MAx mem used: "+itos(MemoryPool::max_memory)); + print_line("Dvectors: " + itos(MemoryPool::allocs_used)); + print_line("Mem used: " + itos(MemoryPool::total_memory)); + print_line("MAx mem used: " + itos(MemoryPool::max_memory)); PoolVector<int> ints; ints.resize(20); @@ -489,8 +471,8 @@ MainLoop* test() { { PoolVector<int>::Write w; w = ints.write(); - for(int i=0;i<ints.size();i++) { - w[i]=i; + for (int i = 0; i < ints.size(); i++) { + w[i] = i; } } @@ -498,15 +480,14 @@ MainLoop* test() { { PoolVector<int>::Read r = posho.read(); - for(int i=0;i<posho.size();i++) { - print_line(itos(i)+" : " +itos(r[i])); + for (int i = 0; i < posho.size(); i++) { + print_line(itos(i) + " : " + itos(r[i])); } } - print_line("later Dvectors: "+itos(MemoryPool::allocs_used)); - print_line("later Mem used: "+itos(MemoryPool::total_memory)); - print_line("Mlater Ax mem used: "+itos(MemoryPool::max_memory)); - + print_line("later Dvectors: " + itos(MemoryPool::allocs_used)); + print_line("later Mem used: " + itos(MemoryPool::total_memory)); + print_line("Mlater Ax mem used: " + itos(MemoryPool::max_memory)); return NULL; @@ -519,29 +500,27 @@ MainLoop* test() { String test = cmdlargs.back()->get(); - FileAccess *fa = FileAccess::open(test,FileAccess::READ); + FileAccess *fa = FileAccess::open(test, FileAccess::READ); if (!fa) { - ERR_EXPLAIN("Could not open file: "+test); + ERR_EXPLAIN("Could not open file: " + test); ERR_FAIL_V(NULL); } - Vector<uint8_t> buf; int flen = fa->get_len(); - buf.resize(fa->get_len()+1); - fa->get_buffer(&buf[0],flen); - buf[flen]=0; - + buf.resize(fa->get_len() + 1); + fa->get_buffer(&buf[0], flen); + buf[flen] = 0; String code; - code.parse_utf8((const char*)&buf[0]); + code.parse_utf8((const char *)&buf[0]); GetClassAndNamespace getclass; if (getclass.parse(code)) { - print_line("Parse error: "+getclass.get_error()); + print_line("Parse error: " + getclass.get_error()); } else { - print_line("Found class: "+getclass.get_class()); + print_line("Found class: " + getclass.get_class()); } return NULL; @@ -552,35 +531,33 @@ MainLoop* test() { List<StringName> tl; ClassDB::get_class_list(&tl); - - for (List<StringName>::Element *E=tl.front();E;E=E->next()) { + for (List<StringName>::Element *E = tl.front(); E; E = E->next()) { Vector<uint8_t> m5b = E->get().operator String().md5_buffer(); hashes.push_back(hashes.size()); - } //hashes.resize(50); - for(int i=nearest_shift(hashes.size());i<20;i++) { + for (int i = nearest_shift(hashes.size()); i < 20; i++) { - bool success=true; - for(int s=0;s<10000;s++) { + bool success = true; + for (int s = 0; s < 10000; s++) { Set<uint32_t> existing; - success=true; + success = true; - for(int j=0;j<hashes.size();j++) { + for (int j = 0; j < hashes.size(); j++) { - uint32_t eh = ihash2(ihash3(hashes[j]+ihash(s)+s))&((1<<i)-1); + uint32_t eh = ihash2(ihash3(hashes[j] + ihash(s) + s)) & ((1 << i) - 1); if (existing.has(eh)) { - success=false; + success = false; break; } existing.insert(eh); } if (success) { - print_line("success at "+itos(i)+"/"+itos(nearest_shift(hashes.size()))+" shift "+itos(s)); + print_line("success at " + itos(i) + "/" + itos(nearest_shift(hashes.size())) + " shift " + itos(s)); break; } } @@ -590,47 +567,42 @@ MainLoop* test() { print_line("DONE"); - - return NULL; } { - //print_line("NUM: "+itos(237641278346127)); - print_line("NUM: "+itos(-128)); + print_line("NUM: " + itos(-128)); return NULL; - } - { - Vector3 v(1,2,3); + Vector3 v(1, 2, 3); v.normalize(); - float a=0.3; + float a = 0.3; //Quat q(v,a); - Basis m(v,a); + Basis m(v, a); - Vector3 v2(7,3,1); + Vector3 v2(7, 3, 1); v2.normalize(); - float a2=0.8; + float a2 = 0.8; //Quat q(v,a); - Basis m2(v2,a2); + Basis m2(v2, a2); - Quat q=m; - Quat q2=m2; + Quat q = m; + Quat q2 = m2; Basis m3 = m.inverse() * m2; - Quat q3 = (q.inverse() * q2);//.normalized(); + Quat q3 = (q.inverse() * q2); //.normalized(); print_line(Quat(m3)); print_line(q3); - print_line("before v: "+v+" a: "+rtos(a)); - q.get_axis_and_angle(v,a); - print_line("after v: "+v+" a: "+rtos(a)); + print_line("before v: " + v + " a: " + rtos(a)); + q.get_axis_and_angle(v, a); + print_line("after v: " + v + " a: " + rtos(a)); } return NULL; @@ -638,98 +610,91 @@ MainLoop* test() { List<String> args; args.push_back("-l"); - Error err = OS::get_singleton()->execute("/bin/ls",args,true,NULL,&ret); - print_line("error: "+itos(err)); + Error err = OS::get_singleton()->execute("/bin/ls", args, true, NULL, &ret); + print_line("error: " + itos(err)); print_line(ret); return NULL; Basis m3; - m3.rotate(Vector3(1,0,0),0.2); - m3.rotate(Vector3(0,1,0),1.77); - m3.rotate(Vector3(0,0,1),212); + m3.rotate(Vector3(1, 0, 0), 0.2); + m3.rotate(Vector3(0, 1, 0), 1.77); + m3.rotate(Vector3(0, 0, 1), 212); Basis m32; m32.set_euler(m3.get_euler()); - print_line("ELEULEEEEEEEEEEEEEEEEEER: "+m3.get_euler()+" vs "+m32.get_euler()); - + print_line("ELEULEEEEEEEEEEEEEEEEEER: " + m3.get_euler() + " vs " + m32.get_euler()); return NULL; { Dictionary d; - d["momo"]=1; - Dictionary b=d; - b["44"]=4; + d["momo"] = 1; + Dictionary b = d; + b["44"] = 4; } - - return NULL; - print_line("inters: "+rtos(Geometry::segment_intersects_circle(Vector2(-5,0),Vector2(-2,0),Vector2(),1.0))); - + print_line("inters: " + rtos(Geometry::segment_intersects_circle(Vector2(-5, 0), Vector2(-2, 0), Vector2(), 1.0))); - - print_line("cross: "+Vector3(1,2,3).cross(Vector3(4,5,7))); - print_line("dot: "+rtos(Vector3(1,2,3).dot(Vector3(4,5,7)))); - print_line("abs: "+Vector3(-1,2,-3).abs()); - print_line("distance_to: "+rtos(Vector3(1,2,3).distance_to(Vector3(4,5,7)))); - print_line("distance_squared_to: "+rtos(Vector3(1,2,3).distance_squared_to(Vector3(4,5,7)))); - print_line("plus: "+(Vector3(1,2,3)+Vector3(Vector3(4,5,7)))); - print_line("minus: "+(Vector3(1,2,3)-Vector3(Vector3(4,5,7)))); - print_line("mul: "+(Vector3(1,2,3)*Vector3(Vector3(4,5,7)))); - print_line("div: "+(Vector3(1,2,3)/Vector3(Vector3(4,5,7)))); - print_line("mul scalar: "+(Vector3(1,2,3)*2)); - print_line("premul scalar: "+(2*Vector3(1,2,3))); - print_line("div scalar: "+(Vector3(1,2,3)/3.0)); - print_line("length: "+rtos(Vector3(1,2,3).length())); - print_line("length squared: "+rtos(Vector3(1,2,3).length_squared())); - print_line("normalized: "+Vector3(1,2,3).normalized()); - print_line("inverse: "+Vector3(1,2,3).inverse()); + print_line("cross: " + Vector3(1, 2, 3).cross(Vector3(4, 5, 7))); + print_line("dot: " + rtos(Vector3(1, 2, 3).dot(Vector3(4, 5, 7)))); + print_line("abs: " + Vector3(-1, 2, -3).abs()); + print_line("distance_to: " + rtos(Vector3(1, 2, 3).distance_to(Vector3(4, 5, 7)))); + print_line("distance_squared_to: " + rtos(Vector3(1, 2, 3).distance_squared_to(Vector3(4, 5, 7)))); + print_line("plus: " + (Vector3(1, 2, 3) + Vector3(Vector3(4, 5, 7)))); + print_line("minus: " + (Vector3(1, 2, 3) - Vector3(Vector3(4, 5, 7)))); + print_line("mul: " + (Vector3(1, 2, 3) * Vector3(Vector3(4, 5, 7)))); + print_line("div: " + (Vector3(1, 2, 3) / Vector3(Vector3(4, 5, 7)))); + print_line("mul scalar: " + (Vector3(1, 2, 3) * 2)); + print_line("premul scalar: " + (2 * Vector3(1, 2, 3))); + print_line("div scalar: " + (Vector3(1, 2, 3) / 3.0)); + print_line("length: " + rtos(Vector3(1, 2, 3).length())); + print_line("length squared: " + rtos(Vector3(1, 2, 3).length_squared())); + print_line("normalized: " + Vector3(1, 2, 3).normalized()); + print_line("inverse: " + Vector3(1, 2, 3).inverse()); { - Vector3 v(4,5,7); + Vector3 v(4, 5, 7); v.normalize(); - print_line("normalize: "+v); + print_line("normalize: " + v); } { - Vector3 v(4,5,7); - v+=Vector3(1,2,3); - print_line("+=: "+v); + Vector3 v(4, 5, 7); + v += Vector3(1, 2, 3); + print_line("+=: " + v); } { - Vector3 v(4,5,7); - v-=Vector3(1,2,3); - print_line("-=: "+v); + Vector3 v(4, 5, 7); + v -= Vector3(1, 2, 3); + print_line("-=: " + v); } { - Vector3 v(4,5,7); - v*=Vector3(1,2,3); - print_line("*=: "+v); + Vector3 v(4, 5, 7); + v *= Vector3(1, 2, 3); + print_line("*=: " + v); } { - Vector3 v(4,5,7); - v/=Vector3(1,2,3); - print_line("/=: "+v); + Vector3 v(4, 5, 7); + v /= Vector3(1, 2, 3); + print_line("/=: " + v); } { - Vector3 v(4,5,7); - v*=2.0; - print_line("scalar *=: "+v); + Vector3 v(4, 5, 7); + v *= 2.0; + print_line("scalar *=: " + v); } { - Vector3 v(4,5,7); - v/=2.0; - print_line("scalar /=: "+v); + Vector3 v(4, 5, 7); + v /= 2.0; + print_line("scalar /=: " + v); } - - #if 0 print_line(String("C:\\momo\\.\\popo\\..\\gongo").simplify_path()); print_line(String("res://../popo/..//gongo").simplify_path()); @@ -811,7 +776,5 @@ MainLoop* test() { #endif return NULL; - } - } |
