Optimal high-dimensional and nonparametric distributed testing under communication constraints