Giter Club home page Giter Club logo

std-swapper's Introduction

STD Swapper

STD Swapper is a small C utility for swapping out standard input and output to temporary files, and restoring the standard input and output afterwards. Its primary utility may be unit tests, though users may find other applications.

Motivation

When writing unit tests, I found the need to temporarily re-route standard input and output. This proved to be slightly complicated and error-prone, and called for an independent implementation and test pipeline.

Use

Most of the time, you probably want to redirect stdout and stdin simultaneously, and provide some input for stdin, because the primary use-case, at least for what I envision, is for testing function responses to simulated user input and hiding printing to the console.

To do that, first initialize the swapper, then use the convenience method to redirect both stdin and stdout and create the temporary input file. After you call the function that will be reading from stdin, use the convenience method to restore both.

Example:

void testSomething() {

    StdSwapper_Init();
    StdSwapper_SetAllStdWithInputOf("1");
    /* Say this function returns an int conversion of the user input */
    int test_result = myPromptFunction();
    StdSwapper_RestoreAllStd();
    assert(test_result == 1);

    StdSwapper_SetAllStdWithInputOf("a string");
    char buf[100];
    gets(buf);
    test_result = strcmp("a_string", buf);
    assert(test_result == 1);
    StdSwapper_RestoreAllStd();

    StdSwapper_DeInit();
}

Installation

Copy std_swapper.c and std_swapper.h into your source folder. Compile them with the rest of your program (or, more likely, when you compile your tests.)

Credits

Feel free to use this for any purpose. std_swapper is MIT license. CuTest, used only in testing this project, is GPL v2.

Author: klm127 Date: 1/23/2023

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.