global distance test algorithm