Skip to content

Instantly share code, notes, and snippets.

@amaembo
Created September 11, 2015 11:57
Show Gist options
  • Save amaembo/2687d2b1dbd8ae6b3df4 to your computer and use it in GitHub Desktop.
Save amaembo/2687d2b1dbd8ae6b3df4 to your computer and use it in GitHub Desktop.
Recursion test
public class RecursionTest {
static int count = 0;
public static void staticMethod() {
count++;
staticMethod();
}
public static void staticMethod(int a) {
count++;
staticMethod(a);
}
public static void staticMethod(int a, int b) {
count++;
staticMethod(a, b);
}
public static void staticMethod(int a, int b, int c) {
count++;
staticMethod(a, b, c);
}
public static void staticMethod(int a, int b, int c, int d) {
count++;
staticMethod(a, b, c, d);
}
public void instanceMethod() {
count++;
instanceMethod();
}
public void instanceMethod(int a) {
count++;
instanceMethod(a);
}
public void instanceMethod(int a, int b) {
count++;
instanceMethod(a, b);
}
public void instanceMethod(int a, int b, int c) {
count++;
instanceMethod(a, b, c);
}
public void instanceMethod(int a, int b, int c, int d) {
count++;
instanceMethod(a, b, c, d);
}
public static void test(String name, Runnable r) {
count = 0;
try {
r.run();
}
catch(StackOverflowError e) {
System.out.println(name+": "+count);
return;
}
throw new InternalError();
}
public static void main(String[] args) {
test("Static/0", new Runnable() {
@Override
public void run() {
staticMethod();
}
});
test("Static/1", new Runnable() {
@Override
public void run() {
staticMethod(1);
}
});
test("Static/2", new Runnable() {
@Override
public void run() {
staticMethod(1,2);
}
});
test("Static/3", new Runnable() {
@Override
public void run() {
staticMethod(1,2,3);
}
});
test("Static/4", new Runnable() {
@Override
public void run() {
staticMethod(1,2,3,4);
}
});
test("Instance/0", new Runnable() {
@Override
public void run() {
new RecursionTest().instanceMethod();
}
});
test("Instance/1", new Runnable() {
@Override
public void run() {
new RecursionTest().instanceMethod(1);
}
});
test("Instance/2", new Runnable() {
@Override
public void run() {
new RecursionTest().instanceMethod(1,2);
}
});
test("Instance/3", new Runnable() {
@Override
public void run() {
new RecursionTest().instanceMethod(1,2,3);
}
});
test("Instance/4", new Runnable() {
@Override
public void run() {
new RecursionTest().instanceMethod(1,2,3,4);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment