Skip to content

Instantly share code, notes, and snippets.

@zed

zed/.gitignore Secret

Last active June 10, 2016 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zed/dd44ade13d313ceb8ba8e384ba1ff1ac to your computer and use it in GitHub Desktop.
Save zed/dd44ade13d313ceb8ba8e384ba1ff1ac to your computer and use it in GitHub Desktop.
/dummy
/infile.txt
/outfile.txt

The gist tries to reproduce the issue from the SO question about the difference in the output between executing the same command in a shell and in python.

  1. Install git, make, g++, env, bash, python, diff

    For example, on Windows, you could install "git for Windows", "GnuWin32 make", "MinGW g++" and configure %PATH% correspondingly

  2. Clone gist

     $ git clone https://gist.github.com/zed/dd44ade13d313ceb8ba8e384ba1ff1ac so-q
     $ cd so-q
    
  3. Generate input data, build dummy c++, run the test:

     $ make
    

If you see: "Files infile.txt and outfile.txt are identical" then there is no difference (the test has failed to reproduce the issue).

/** $ g++ dummy.cc -o dummy */
#include <iostream>
int main()
{
int size, count, a, b;
std::cin >> size;
std::cin >> count;
std::cout << size << " " << count << std::endl;
for (int i = 0; i < count; ++i)
{
std::cin >> a >> b;
std::cout << a << " " << b << std::endl;
}
}
n = 100000
print(1, n)
for i in range(100000):
print(i, i+i)
test: user2346536.py dummy infile.txt
env PATH=.:"$$PATH" python $<
diff -s infile.txt outfile.txt
dummy: dummy.cc
g++ $< -o $@
infile.txt: generate-infile.py
python $< > $@
# from http://stackoverflow.com/questions/37734470/why-is-subprocess-run-output-different-from-shell-output-of-same-command
import subprocess
with open('infile.txt', 'rb') as test_file:
stdin = test_file.read()
p = subprocess.run(['dummy'], input=stdin, stdout=subprocess.PIPE)
out = p.stdout
if stdin == out:
print('OK')
with open('outfile.txt', "wb") as text_file:
text_file.write(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment