user188276
user188276

Reputation:

how to test C or C++ snippet quickly?

I am using Ubuntu and Eclipse as an IDE for C/C++.

I currently have a big project in Eclipse. Sometimes, I want to test some small functions written in C/C++ but I don't want to re-create a new project in Eclipse. It is much time consuming and slow. I want to ask if there is any better way to do this ?

(In the past, I usually used a combination of GEDIT and GCC from the shell, but I really like the auto-completion or intellisense feature in Eclipse, which GEDIT does not have. I have also tried Scribes but it does not have a full intellisense feature like Eclipse)

Upvotes: 9

Views: 7564

Answers (6)

Shinonome Tera tanuki
Shinonome Tera tanuki

Reputation: 11

If you don't think it's a good idea to write your code snippet into a single file and compile it, unit testing might be a suitable alternative for you.

Due to the length constraints, I cannot elaborate on the technical details of unit testing. However, the following is a usable example code.

// test_f.cpp
#include <gtest/gtest.h>
#include "f.h"

// test the function f()
TEST(FTest, PositiveNumbers) {
    EXPECT_EQ(f(2), 4);
    EXPECT_EQ(f(3), 9);
}

TEST(FTest, Zero) {
    EXPECT_EQ(f(0), 0);
}

TEST(FTest, NegativeNumbers) {
    EXPECT_EQ(f(-2), 4);
    EXPECT_EQ(f(-3), 9);
}

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

(I suppose that the file you want to test is f.cpp and its header file is f.h)

This code performs three unit tests, calling f() with different parameters and providing the expected output in the second argument of EXPECT_EQ.

The function f() is expected to compute the square of the input parameter.

This code uses Google Test as the unit testing library.

You can use other libraries as your unit testing framework.

For more information, see the following link:

Sorry for my poor English.

Upvotes: 1

Namit Sinha
Namit Sinha

Reputation: 1445

http://www.compileonline.com I found this Site more useful than ideone or codepad because it supports more languages than codepad and you can see the output of you code on an adjacent window you can also provide Standard Inputs and command line arguments and you can also access a file input.txt in your program.

CompileOnlineScreenShot

Upvotes: 0

Sebastian Mach
Sebastian Mach

Reputation: 39109

This method works without an internet connection and without exposing your code.

<ctrl>+<alt>+T                        <-- 0) opens a terminal

vi test.cc                            <-- 1) hackery
...
g++ -Wall -Wextra test.cc && ./a.out  <-- 2) compile + run
rm test.cc                            <-- 3) clean up (optional)

Replace vi with your favourite editor or cat. Can't be less obtrusive.

Some editors like SciTE have some very basic code completion (btw btw: SciTE has shortcuts to directly compile and run code from within the editor).

Btw: QtCreator gives some decent "intellisense", and the project files are minimal. A single project file line is enough for such one-function-test.


unkulunkulu points out that you can also replace step 2 like this (there should better be no Makefile in your try-out folder; could conflict with existing targets in that):

<ctrl>+<alt>+T                  <-- 0) opens a terminal

vi test.cc                      <-- 1) hackery
...
make test && test               <-- 2) compile + run
rm test.cc                      <-- 3) clean up (optional)

It has the tiny disadvantage that telling g++ about extra arguments (like -Wall or -std=c++0x is a bit more obtrusive).

Upvotes: 7

pmg
pmg

Reputation: 108978

You can use tcc as a C script engine.

$ cat tcctest.c
#!/usr/bin/tcc -run
#include <stdio.h>
int main(void) {
    printf("Hello, tcc!\n");
    return 0;
}
$ chmod u+x tcctest.c
$ ./tcctest.c
Hello, tcc!

Upvotes: 1

user744186
user744186

Reputation:

I will advise you to use gedit with the embeded terminal plugin.It allows quick compiling through the embeded terminal.Perfect for quick testing.

Upvotes: 1

Alok Save
Alok Save

Reputation: 206566

Use online compiler like Ideone or Codepad.
Ofcourse, they dont provide you auto code completion feature & other fancy features but that is the price you pay for quick & easy way of checking stand alone functions.

Upvotes: 13

Related Questions