[prog] C programming - capturing information sent to stdout

Mary mary-linuxchix at puzzling.org
Thu Jul 14 10:52:58 EST 2005


On Thu, Jul 14, 2005, Mary wrote:
> I would use it mostly for debugging and for enforcing pre-conditions
> on code that I expect other people to use (like a library I write or a
> group project) so that people find out nice and quickly when they use
> a method in a way I don't expect. Like much programming, this may
> happen backwards: you only realise a pre-condition exists when someone
> tries something you never expected ("oh! you tried it with UPPERCASE?
> I didn't expect that!") and you then put the assert in to make sure
> noone else makes the same mistake.

It's also commonly used in unit testing (automated testing of small
pieces of your code). Here's a trivial example in Python (which has a
unit testing framework anyway, so you wouldn't usually do it without the
framework):

def add(x, y):
    """
    Add x and y together and return the result
    """
    return x + y

def testAdd():
    """
    Unit test the add() function.
    """
    assert add(2, 3) == 5
    assert add(6, 7) == 13
    assert add(0, -1) == -1

-Mary


More information about the Programming mailing list