deviceCount is 0 only in case of no CUDA-capable devices found in a workstation. Found devices are indexed starting from 0. Other than that, you are right - one variable is enough. The necessary change is:
} else if (CUDA_TEST_DEVICE >= 0) {

	  // Use the device assigned in build configuration; but with a lower priority
	  device = CUDA_TEST_DEVICE;
  }