How about making this more stringent?  E.g.

    assert_equal(x.nnz, int(density*np.prod(x.shape)))