You don’t know that you failed, you are speculating on how difficult the test was. You might have done much better than you thought. And, since it was a mid term and not the final, you will be better prepared next time, as you even mention. I have had tests that confounded me even though I thought I was prepared, usually because I had emphasized the wrong areas in my studying. Definitely talk to the teacher after you get the test back; that’s what office hours are for!