Skip to content

Commit

Permalink
Add new blog post "Interviewee's T-tests"
Browse files Browse the repository at this point in the history
Also reformat the blog page a bit
  • Loading branch information
elihunter173 committed Oct 17, 2024
1 parent 016d103 commit 1889322
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 3 deletions.
121 changes: 121 additions & 0 deletions content/blog/interviewees_t_test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
+++
title = "Interviewee's T-tests"
date = 2024-10-17
+++

I've been running a lot of coding interviews lately, especially for people coming directly from university. And it strikes me that many candidates will keep only a single informal test that they write and rewrite as they want to check things about their code.

This always sort of bothers me because when you delete a test the first time it passes, you lose the ability to catch regressions. But also because what a "test" is is so informal when you're rewriting the same piece of code over and over, many candidates don't really think about what exactly the goal is of any given test and won't comprehensively test their code.

The easy and practical way to avoid these pitfalls is to keep your old tests and copy-and-paste a new one instead of rewriting the same one over and over. But I'm going to propose a swaggier solution that I call **Interviewee's T-tests**.

The idea with T-tests is that they are an _extremely_ small and easy to understand test framework that you could write from memory and explain in less than a minute.

Below I've written some T-tests in all the programming languages I feel comfortable doing interviews in, with their output below the code. But it should be easy to write versions in your programming language of choice.

## Rust
```rust
fn test(name: &str, t: impl FnOnce() + std::panic::UnwindSafe) {
println!("=== {name}");
if std::panic::catch_unwind(t).is_ok() {
println!("=== PASS\n");
} else {
println!("=== FAIL\n");
}
}

fn main() {
test("failing", || {
assert!(false);
});
test("passing", || {
assert!(true);
});
}
```

```
=== failing
thread 'main' panicked at src/main.rs:12:9:
assertion failed: false
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
=== FAIL
=== passing
=== PASS
```

## Python

```python
import traceback

def test(t):
print(f"=== {t.__name__}")
try:
t()
print("=== PASS\n")
except Exception as e:
traceback.print_exception(e)
print("=== FAIL\n")

@test
def failing():
assert False

@test
def passing():
assert True
```

```
=== failing
Traceback (most recent call last):
File "/Users/eli.hunter/t.py", line 6, in test
t()
File "/Users/eli.hunter/t.py", line 14, in failing
assert False
AssertionError
=== FAIL
=== passing
=== PASS
```

## C
```c
#include <stdio.h>

int failing() {
return 1;
}

int passing() {
return 0;
}

void test(char* name, int (*t) (void)) {
printf("=== %s\n", name);
int rtn = t();
if (rtn == 0) {
puts("=== PASS\n");
} else {
printf("returned %d\n", rtn);
puts("=== FAIL\n");
}
}

int main() {
test("failing", failing);
test("passing", passing);
}
```
```
=== failing
returned 1
=== FAIL

=== passing
=== PASS
```
1 change: 0 additions & 1 deletion content/blog/toothpaste_reviews_2020.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
+++
title = "Toothpaste Reviews 2020"
description = "A silly review of a bunch of travel toothpastes I had lying around."
date = 2020-09-07
+++

Expand Down
6 changes: 4 additions & 2 deletions templates/blog/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
<h1 class="title">{{ section.title }}</h1>
{{ section.content | safe }}

<ul style="list-style: none;">
{% for page in section.pages %}
<h2><a href="{{ page.path | safe }}">{{ page.title }}</a></h2>
{{ page.description }}
<li>{{ page.date }} – <a href="{{ page.path | safe }}">{{ page.title }}</a><li>
{% endfor %}
</ul>


{% endblock content %}
1 change: 1 addition & 0 deletions templates/blog/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{% block content %}
<header>
<h1 class="title">{{ page.title }}</h1>
{{ page.date }}
</header>

{{ page.content | safe }}
Expand Down

0 comments on commit 1889322

Please sign in to comment.