diff --git a/Examples/test-suite/overload_template.i b/Examples/test-suite/overload_template.i index 01a1dd633..0bf1b0070 100644 --- a/Examples/test-suite/overload_template.i +++ b/Examples/test-suite/overload_template.i @@ -17,3 +17,112 @@ template %template(max) max; %template(max) max; +// Mix template overloading with plain function overload +// Mix 1 +%inline %{ + int mix1(const char* msg) { return 101; } + template int mix1(T t, const T& tt) { return 102; } + template int mix1(T t) { return 103; } +%} +%template(mix1) mix1; + +// Mix 2 +%inline %{ + template int mix2(T t, const T& tt) { return 102; } + int mix2(const char* msg) { return 101; } + template int mix2(T t) { return 103; } +%} +%template(mix2) mix2; + +// Mix 3 +%inline %{ + template int mix3(T t, const T& tt) { return 102; } + template int mix3(T t) { return 103; } + int mix3(const char* msg) { return 101; } +%} +%template(mix3) mix3; + + +// overloaded by number of templated parameters +// Combination 1 +%inline %{ +template int overtparams1(T t) { return 10; } +template int overtparams1(T t, U u) { return 20; } +%} + +%template(overtparams1) overtparams1; +%template(overtparams1) overtparams1; + + +// Combination 2 +%inline %{ +template int overtparams2(T t) { return 30; } +template int overtparams2(T t, U u) { return 40; } +%} + +%template(overtparams2) overtparams2; + + +// Combination 3 +%inline %{ +template int overloaded(T t) { return 50; } +int overloaded() { return 60; } +template int overloaded(T t, U u) { return 70; } +%} + +%template(overloaded) overloaded; + +// Combination 4 +%inline %{ +int overloadedagain(const char* msg) { return 80; } +template int overloadedagain() { return 90; } +template int overloadedagain(T t, U u) { return 100; } +%} + +%template(overloadedagain) overloadedagain; + +// simple specialization +%inline %{ +template void xyz() {} +template<> void xyz() {} +void xyz() {} +%} + +// We can have xyz(); xyz(); xyz(); in C++, but can't have this type of overloading in target language, so we need to do some renaming +%template(xyz_double) xyz; +%template(xyz_int) xyz; + + +// specializations +%inline %{ +template int specialization(T t) { return 200; } +template int specialization(T t, U u) { return 201; } +template<> int specialization(int t) { return 202; } +template<> int specialization(double t) { return 203; } +template<> int specialization(int t, int u) { return 204; } +template<> int specialization(double t, double u) { return 205; } +%} + +%template(specialization) specialization; +%template(specialization) specialization; +%template(specialization) specialization; +%template(specialization) specialization; +%template(specialization) specialization; + + +// a bit of everything +%inline %{ +int overload(const char *c) { return 0; } +template int overload(T t) { return 10; } +template int overload(T t, const T &tref) { return 20; } +template int overload(T t, const char *c) { return 30; } +template<> int overload(double t, const char *c) { return 40; } +int overload() { return 50; } + +class Klass {}; +%} + +%template(overload) overload; +%template(overload) overload; +%template(overload) overload; + diff --git a/Examples/test-suite/python/overload_template_runme.py b/Examples/test-suite/python/overload_template_runme.py index 1398dd739..8bc10b67a 100644 --- a/Examples/test-suite/python/overload_template_runme.py +++ b/Examples/test-suite/python/overload_template_runme.py @@ -3,3 +3,111 @@ f = foo() a = max(3,4) b = max(3.4,5.2) + +# mix 1 +if (mix1("hi") != 101): + raise RuntimeError, ("mix1(const char*)") + +if (mix1(1.0, 1.0) != 102): + raise RuntimeError, ("mix1(double, const double &)") + +if (mix1(1.0) != 103): + raise RuntimeError, ("mix1(double)") + +# mix 2 +if (mix2("hi") != 101): + raise RuntimeError, ("mix2(const char*)") + +if (mix2(1.0, 1.0) != 102): + raise RuntimeError, ("mix2(double, const double &)") + +if (mix2(1.0) != 103): + raise RuntimeError, ("mix2(double)") + +# mix 3 +if (mix3("hi") != 101): + raise RuntimeError, ("mix3(const char*)") + +if (mix3(1.0, 1.0) != 102): + raise RuntimeError, ("mix3(double, const double &)") + +if (mix3(1.0) != 103): + raise RuntimeError, ("mix3(double)") + +# Combination 1 +if (overtparams1(100) != 10): + raise RuntimeError, ("overtparams1(int)") + +if (overtparams1(100.0, 100) != 20): + raise RuntimeError, ("overtparams1(double, int)") + +# Combination 2 +if (overtparams2(100.0, 100) != 40): + raise RuntimeError, ("overtparams2(double, int)") + +# Combination 3 +if (overloaded() != 60): + raise RuntimeError, ("overloaded()") + +if (overloaded(100.0, 100) != 70): + raise RuntimeError, ("overloaded(double, int)") + +# Combination 4 +if (overloadedagain("hello") != 80): + raise RuntimeError, ("overloadedagain(const char *)") + +if (overloadedagain() != 90): + raise RuntimeError, ("overloadedagain(double)") + +# specializations +if (specialization(10) != 202): + raise RuntimeError, ("specialization(int)") + +if (specialization(10.0) != 203): + raise RuntimeError, ("specialization(double)") + +if (specialization(10, 10) != 204): + raise RuntimeError, ("specialization(int, int)") + +if (specialization(10.0, 10.0) != 205): + raise RuntimeError, ("specialization(double, double)") + +if (specialization("hi", "hi") != 201): + raise RuntimeError, ("specialization(const char *, const char *)") + + +# simple specialization +xyz() +xyz_int() +xyz_double() + +# a bit of everything +if (overload("hi") != 0): + raise RuntimeError, ("overload()") + +if (overload(1) != 10): + raise RuntimeError, ("overload(int t)") + +if (overload(1, 1) != 20): + raise RuntimeError, ("overload(int t, const int &)") + +if (overload(1, "hello") != 30): + raise RuntimeError, ("overload(int t, const char *)") + +k = Klass() +if (overload(k) != 10): + raise RuntimeError, ("overload(Klass t)") + +if (overload(k, k) != 20): + raise RuntimeError, ("overload(Klass t, const Klass &)") + +if (overload(k, "hello") != 30): + raise RuntimeError, ("overload(Klass t, const char *)") + +if (overload(10.0, "hi") != 40): + raise RuntimeError, ("overload(double t, const char *)") + +if (overload() != 50): + raise RuntimeError, ("overload(const char *)") + +