ArticleslgStudy

computer science

Sanity check

Sanity check is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Sanity check rather than just read about it. In short: A sanity check or sanity test is a basic test to quickly evaluate whether a claim or the result of a calculation can possibly be true. It is a simple check to see if the produced material is rational (that the material's creator was thinking rationally, applying sanity).

Key takeaways

  • Sanity check belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Sanity check to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Sanity check from memory before moving on to harder problems.

Reference excerpt

A sanity check or sanity test is a basic test to quickly evaluate whether a claim or the result of a calculation can possibly be true. It is a simple check to see if the produced material is rational (that the material's creator was thinking rationally, applying sanity). The point of a sanity test is to rule out certain classes of obviously false results, not to catch every possible error. A rule-of-thumb or back-of-the-envelope calculation may be checked to perform the test. The advantage of performing an initial sanity test is that of speedily evaluating basic function. In arithmetic, for example, when multiplying by 9, using the divisibility rule for 9 to verify that the sum of digits of the result is divisible by 9 is a sanity test—it will not catch every multiplication error, but is a quick and simple method to discover many possible errors. In computer science, a sanity test is a very brief run-through of the functionality of a computer program, system, calculation, or other analysis, to assure that part of the system or methodology works roughly as expected. This is often prior to a more exhaustive round of testing.

Use in different fields

Mathematical A sanity test can refer to various orders of magnitude and other simple rule-of-thumb devices applied to cross-check mathematical calculations. For example:

If one were to attempt to square 738 and calculated 54,464, a quick sanity check could show that this result cannot be true. Consider that 700 < 738, yet 7002 = 72 × 1002 = 490,000 > 54,464. Since squaring positive integers preserves their inequality, the result cannot be true, and so the calculated result is incorrect. The correct answer, 7382 = 544,644, is more than 10 times higher than 54,464. In multiplication, 918 × 155 is not 142,135 since 918 is divisible by three but 142,135 is not (digits add up to 16, not a multiple of three). Also, the product must end in the same digit as the product of end-digits: 8 × 5 = 40, but 142,135 does not end in "0" like "40", while the correct answer does: 918 × 155 = 142,290. An even quicker check is that the product of even and odd numbers is even, whereas 142,135 is odd.

Physical Dimensional analysis may be used as a sanity check of physical equations: the two sides of any equation must be commensurable or have the same dimensions. A person who has calculated the power output of a car to be 700 kJ may have omitted a factor, since the unit joules is a measure of energy, not power (energy per unit time). When determining physical properties, comparing to known or similar substances will often yield insight on whether the result is reasonable. For instance, most metals sink in water, so the density of most metals should be greater than that of water (~1000 kg/m3). Fermi estimates will often provide insight on the order of magnitude of an expected value.

Software development

In software development, a sanity test (a form of software testing which offers "quick, broad, and shallow testing") evaluates the result of a subset of application functionality to determine whether it is possible and reasonable to proceed with further testing of the entire application. Sanity tests may sometimes be used interchangeably with smoke tests insofar as both terms denote tests which determine whether it is possible and reasonable to continue testing further. On the other hand, a distinction is sometimes made that a smoke test is a non-exhaustive test that ascertains whether the most crucial functions of a programme work before proceeding with further testing whereas a sanity test refers to whether specific functionality such as a particular bug fix works as expected without testing the wider functionality of the software. In other words, a sanity test determines whether the intended result of a code change works correctly while a smoke test ensures that nothing else important was broken in the process. Sanity testing and smoke testing avoid wasting time and effort by quickly determining whether an application is too flawed to merit more rigorous QA testing, but needs more developer debugging. Groups of sanity tests are often bundled together for automated unit testing of functions, libraries, or applications prior to merging development code into a testing or trunk version control branch, for automated building, or for continuous integration and continuous deployment. Another common usage of sanity test is to denote checks which are performed within programme code, usually on arguments to functions or returns therefrom, to see if the answers can be assumed to be correct. The more complicated the routine, the more important that its response be checked. The trivial case is checking to see whether the return value of a function indicated success or failure, and to therefore cease further processing upon failure. This return value is actually often itself the result of a sanity check. For example, if the function attempted to open, write to, and close a file, a sanity check may be used to ensure that it did not fail on any of these actions—which is a sanity check often ignored by programmers. These kinds of sanity checks may be used during development for debugging purposes and also to aid in troubleshooting software runtime errors. Sanity checks are also performed upon installation of stable, production software code into a new computing environment to ensure that all dependencies are met, such as a compatible operating system and link libraries. When a computing environment has passed all the sanity checks, it is considered a sane environment for the installation programme to proceed with reasonable expectation of success. A "Hello, World!" program is often used as a sanity test for a development environment similarly. Rather than a complicated script running a set of unit tests, if this simple programme fails to compile or execute, it proves that the supporting environment likely has a configuration problem that will prevent any code from compiling or executing. But if "Hello world" executes, then any problems experienced with other programmes likely can be attributed to errors in that application's code rather than the environment. The Association for Computing Machinery, and software projects such as Android, MediaWiki and Twitter, discourage use of the phrase sanity check in favour of other terms such as confidence test, coherence check, or simply test, as part of a wider attempt to avoid ableist language and increase inclusivity.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Sanity check

Start with the simplest possible case. Write down what Sanity check claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Sanity check before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Sanity check ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Sanity check

In research
Sanity check appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Sanity check in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Sanity check is common in secondary-school and first-year university syllabi. It links to neighbouring topics Error detection and correction, Software testing, so understanding it makes those chapters shorter.
In everyday life
Look for Sanity check outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Sanity check” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Sanity check in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Sanity check means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Sanity check out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Sanity check in simple terms?

A sanity check or sanity test is a basic test to quickly evaluate whether a claim or the result of a calculation can possibly be true. It is a simple check to see if the produced material is rational (that the material's creator was thinking rationally, applying sanity).

Why does Sanity check matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Sanity check?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Sanity check.

Tags

  • Error detection and correction
  • Software testing

Keep exploring