Proof of $f = g \in L^1_{loc}$ if $f$ and $g$ act equally on $C_c^\infty$
Solution 1:
Like Theo said, this is a standard question whose standard answer (as far as I know) uses smoothing by convolution: take an arbitrary compact $K \subset \mathbb{R}^n$, regularize its characteristic function $\chi_K$ by means of a mollifier sequence and use the hypothesis to show that $\int_K \big(f(x)-g(x)\big)\ dx=0$.
I'd like to provide another proof which doesn't rely on convolution (at least not explicitly). The idea behind it is precisely the same, though.
We need two lemmas.
Lemma 1 If a Borel measurable function $f \colon \mathbb{R}^n \to \mathbb{R}$ is such that, for every (compact) rectangle $R$,
$$\int_R f(x)\,dx=0,$$
then $f=0$ almost everywhere.
Proof Start by recording that the hypotheses implicitly tell us $f \in L^1_{\rm{loc}}(\mathbb{R}^n)$ (click).
It is a known fact (see, e.g., Rudin's Real and complex analysis, §2.19) that every open subset of $\mathbb{R}^n$ can be written as the union of a countable collection of rectangles intersecting only at the boundary. So the $\sigma$-algebra generated by the collection of the rectangles is the whole Borel $\sigma$-algebra.
Fix $M >0$ and consider the rectangle $R_M=[-M, M] \times \ldots \times [-M, M]$. The family
$$\mathcal{A}_M=\left\{ B \subset R_M \mid \int_B f(x)\,dx\ \text{makes sense and vanishes}\right\}$$
is a $\lambda$-system (click) of subsets of $R_M$, because
- $R_M \in \mathcal{A}_M$;
- for every $A, B \in \mathcal{A}_M\ \mathrm{s.t.}\ A \subset B$, we have $$\int_{B-A}f(x)\,dx=\int_B f(x)\, dx-\int_A f(x)\, dx=0;$$
- if $A_1 \subset A_2 \subset \ldots \in \mathcal{A}_M$ then, once put $A=\cup_k A_k$, we have $$\int_A f(x)\, dx= \lim_{k \to \infty} \int_{A_k} f(x)\, dx=0$$ by dominated convergence: in fact $\lvert f\chi_{A_k} \rvert \le \lvert f \chi_{R_M}\rvert \in L^1(\mathbb{R}^n)$ (recall that $f \in L^1_{\rm{loc}}$).
$\mathcal{A}_M$ contains all rectangles of $\mathbb{R}^n$ contained in $R_M$ and those obviously form a $\pi$-class (a class of subsets closed for finite intersections): now by the $\pi$-$\lambda$ theorem (click) we can conclude that $\mathcal{A}_M$ contains all Borelian subsets of $R_M$ and thus $f$ vanishes almost everywhere on it. Since $M$ was arbitrary, we infer that $f$ vanishes almost everywhere on $\mathbb{R}^n$. $\square$
The other lemma we need is the existence of bump functions:
Lemma 2 Let $R=[a_1, b_1] \times \ldots \times [a_n, b_n], \varepsilon > 0, R_{\varepsilon}=[a_1-\varepsilon, b_1+\varepsilon] \times \ldots \times [a_n - \varepsilon, b_n + \varepsilon]$. Then there exists a function $b\in C^\infty_c(\mathbb{R}^n)$ s.t. $0\le b \le 1,\ b \equiv 1$ on $R$ and $\mathrm{supp}(b)\subset R_\varepsilon$.
Proof For each $j$ let $$\varphi^{-}_j(t)=\begin{cases} e^{\frac{1}{(a_j-\varepsilon - t) (t-a_j)}} & t \in (a_j-\varepsilon, a_j) \\ 0 & \text{otherwise} \end{cases}.$$ Then put $\psi^{-}(s)=C^{-}_j\int_{a_j-\varepsilon}^s\varphi^{-}(t)\,dt$ where $C_j$ is a costant chosen so that $\psi\le 1$. Now do the same for $b_j$: let $$\varphi^{+}_j(t)=\begin{cases}e^{\frac{1}{(b_j- t) (t-b_j-\varepsilon)}} & t \in (b_j, b_j+\varepsilon) \\ 0 & \text{otherwise} \end{cases}$$ and $\psi^{+}_j(s)=C^{+}_j\int_{b_j+\varepsilon}^s \varphi^{+}(t)\,dt.$
The function $b_j = \psi_j^-\psi_j^+$ is $C^\infty$ and satisfies $0 \le b_j \le 1, b_j \equiv 1$ on $[a_j, b_j]$ and $\mathrm{supp}(b_j) \subset [a_j-\varepsilon, b_j + \varepsilon]$. So $b(x_1 \ldots x_n)=\prod_j b_j(x_j)$ is what we were looking for. $\square$
With those lemmas it is pretty easy to prove the original claim. In fact, let $f, g$ as in the hypothesis and put $F=f-g$. Take a rectangle $R$ and, for every $n\in \mathbb{N}$, a bump function $b_n$ separating $R$ from $R_{n^{-1}}$. Then $b_n \to \chi_{R}$ pointwise, so that $Fb_n \to F\chi_R$ pointwise; also $\lvert Fb_n \rvert \le \lvert F \chi_{R_1}\rvert \in L^1(\mathbb{R}^n)$. By the dominated convergence theorem, we get $$\int_R F(x)\,dx=0.$$ By Lemma 1, $F\equiv 0$ almost everywhere.
Solution 2:
Another way to do this is to let $\phi(x)$ be a smooth cutoff function equal to $1$ on some ball $B(a,r)$. Then your condition implies that the Fourier coefficient $\int_{R^n}(f(x) - g(x))\phi(x)e^{i\xi \cdot x}\,dx$ is zero for all $\xi \in R^n$. By the uniqueness theorem for the Fourier transform, $(f(x) - g(x))\phi(x) = 0$ a.e, so $f(x) = g(x)$ a.e. on the ball $B(a,r)$. This ball was arbitrary so $f(x) = g(x)$ a.e.