From 2e906c472fbca16fec0b9c4b1efa688d96706e02 Mon Sep 17 00:00:00 2001 From: rask24 <70057885+rask24@users.noreply.github.com> Date: Wed, 14 Feb 2024 19:57:54 +0900 Subject: [PATCH] implement 2e2 test (#37) * refacotr unit test structure * update ci * init rspec * implement rspec test * add Rakefile * refacotr * add pain.sh * update ci * add e2e test to ci * add bonus rule to ci * fix ci name --- .github/workflows/ci.yml | 35 +++++-- .rubocop.yml | 2 + Gemfile | 7 ++ Gemfile.lock | 58 +++++++++++ Makefile | 4 +- Rakefile | 16 +++ test/e2e/.rspec | 1 + test/e2e/check.rb | 14 --- test/e2e/helper/command_helper.rb | 13 +++ test/e2e/helper/spec_helper.rb | 98 +++++++++++++++++++ test/e2e/spec/corner_cases_spec.rb | 11 +++ test/e2e/spec/error_cases_spec.rb | 26 +++++ test/e2e/spec/performance_spec.rb | 26 +++++ test/pain.sh | 16 +++ test/{ => unit}/test_check_args.cpp | 0 test/{ => unit}/test_greedy_operation.cpp | 0 test/{ => unit}/test_is_sorted_stack.cpp | 0 test/{ => unit}/test_push_b_segmented.cpp | 0 test/{ => unit}/test_push_stack.cpp | 0 test/{ => unit}/test_reverse_rotate_stack.cpp | 0 test/{ => unit}/test_rotate_stack.cpp | 0 test/{ => unit}/test_set_cost.cpp | 0 test/{ => unit}/test_set_is_target.cpp | 0 .../test_set_min_cost_opt_method.cpp | 0 test/{ => unit}/test_sort.cpp | 0 test/{ => unit}/test_swap_stack.cpp | 0 26 files changed, 305 insertions(+), 22 deletions(-) create mode 100644 .rubocop.yml create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 Rakefile create mode 100644 test/e2e/.rspec delete mode 100644 test/e2e/check.rb create mode 100644 test/e2e/helper/command_helper.rb create mode 100644 test/e2e/helper/spec_helper.rb create mode 100644 test/e2e/spec/corner_cases_spec.rb create mode 100644 test/e2e/spec/error_cases_spec.rb create mode 100644 test/e2e/spec/performance_spec.rb create mode 100755 test/pain.sh rename test/{ => unit}/test_check_args.cpp (100%) rename test/{ => unit}/test_greedy_operation.cpp (100%) rename test/{ => unit}/test_is_sorted_stack.cpp (100%) rename test/{ => unit}/test_push_b_segmented.cpp (100%) rename test/{ => unit}/test_push_stack.cpp (100%) rename test/{ => unit}/test_reverse_rotate_stack.cpp (100%) rename test/{ => unit}/test_rotate_stack.cpp (100%) rename test/{ => unit}/test_set_cost.cpp (100%) rename test/{ => unit}/test_set_is_target.cpp (100%) rename test/{ => unit}/test_set_min_cost_opt_method.cpp (100%) rename test/{ => unit}/test_sort.cpp (100%) rename test/{ => unit}/test_swap_stack.cpp (100%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d9335ce..4fbbed6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Install dependencies run: | python3 -m pip install --upgrade pip setuptools @@ -19,20 +19,43 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v4 with: submodules: recursive - name: Build push_swap run: make - test: - name: test + - name: Clean push_swap + run: make fclean + - name: Build checker + run: make bonus + unit_test: + name: unit_test runs-on: macos-12 steps: - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v4 with: submodules: recursive - name: Build push_swap run: make - - name: Test + - name: Unit test run: make test + e2e_test: + runs-on: macos-12 + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + submodules: recursive + - name: Build push_swap + run: make + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: 3.3.0 + - name: Install dependencies + run: | + gem install bundler + bundle install + - name: Run tests + run: rake test diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..9c5ae4e --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,2 @@ +inherit_gem: + rubocop-shopify: rubocop.yml diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..20116d7 --- /dev/null +++ b/Gemfile @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +gem "rspec" +gem "open3" +gem "rubocop-shopify", require: false diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..a83eca6 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,58 @@ +GEM + remote: https://rubygems.org/ + specs: + ast (2.4.2) + diff-lcs (1.5.1) + json (2.7.1) + language_server-protocol (3.17.0.3) + open3 (0.2.1) + parallel (1.24.0) + parser (3.3.0.5) + ast (~> 2.4.1) + racc + racc (1.7.3) + rainbow (3.1.1) + regexp_parser (2.9.0) + rexml (3.2.6) + rspec (3.13.0) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.0) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.13.0) + rspec-support (3.13.0) + rubocop (1.60.2) + json (~> 2.3) + language_server-protocol (>= 3.17.0) + parallel (~> 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8, < 3.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.30.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.30.0) + parser (>= 3.2.1.0) + rubocop-shopify (2.14.0) + rubocop (~> 1.51) + ruby-progressbar (1.13.0) + unicode-display_width (2.5.0) + +PLATFORMS + arm64-darwin-23 + ruby + +DEPENDENCIES + open3 + rspec + rubocop-shopify + +BUNDLED WITH + 2.5.3 diff --git a/Makefile b/Makefile index 514a2ac..e2a2853 100644 --- a/Makefile +++ b/Makefile @@ -12,8 +12,8 @@ SRC_DIR = src BUILD_DIR = build INC_DIR = include LIBFT_DIR = libft -TEST_DIR = test -GTEST_DIR = test/gtest +TEST_DIR = test/unit +GTEST_DIR = test/unit/gtest TEST_BUILD_DIR = test/build SRC = $(SRC_DIR)/main.c \ diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..dbfcb05 --- /dev/null +++ b/Rakefile @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require "rspec/core/rake_task" + +RSpec::Core::RakeTask.new(:test) do |task| + task.pattern = [ + "test/e2e/spec/corner_cases_spec.rb", + "test/e2e/spec/error_cases_spec.rb", + ] +end + +RSpec::Core::RakeTask.new(:performance) do |task| + task.pattern = [ + "test/e2e/spec/performance_spec.rb", + ] +end diff --git a/test/e2e/.rspec b/test/e2e/.rspec new file mode 100644 index 0000000..c99d2e7 --- /dev/null +++ b/test/e2e/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/test/e2e/check.rb b/test/e2e/check.rb deleted file mode 100644 index fdd6a20..0000000 --- a/test/e2e/check.rb +++ /dev/null @@ -1,14 +0,0 @@ -# frozen_string_literal: true - -a = [1, 2, 3, 4, 5] - -a.permutation.to_a.each do |array| - str = +'./push_swap' - array.each do |item| - str << " #{item}" - end - str << ' | wc -l | bc' - puts str - system str, exception: true - puts "\n" -end diff --git a/test/e2e/helper/command_helper.rb b/test/e2e/helper/command_helper.rb new file mode 100644 index 0000000..0581112 --- /dev/null +++ b/test/e2e/helper/command_helper.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require "open3" + +def execute_push_swap(*args) + stdout, stderr, status = Open3.capture3("./push_swap #{args.join(" ")}") + [stdout, stderr, status] +end + +def execute_checker(*args) + stdout, stderr, status = Open3.capture3("./push_swap #{args.join(" ")} | ./checker #{args.join(" ")}") + [stdout, stderr, status] +end diff --git a/test/e2e/helper/spec_helper.rb b/test/e2e/helper/spec_helper.rb new file mode 100644 index 0000000..1434922 --- /dev/null +++ b/test/e2e/helper/spec_helper.rb @@ -0,0 +1,98 @@ +# frozen_string_literal: true + +# This file was generated by the `rspec --init` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with(:rspec) do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with(:rspec) do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + + # The settings below are suggested to provide a good initial experience + # with RSpec, but feel free to customize to your heart's content. + # # This allows you to limit a spec run to individual examples or groups + # # you care about by tagging them with `:focus` metadata. When nothing + # # is tagged with `:focus`, all examples get run. RSpec also provides + # # aliases for `it`, `describe`, and `context` that include `:focus` + # # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + # config.filter_run_when_matching :focus + # + # # Allows RSpec to persist some state between runs in order to support + # # the `--only-failures` and `--next-failure` CLI options. We recommend + # # you configure your source control system to ignore this file. + # config.example_status_persistence_file_path = "spec/examples.txt" + # + # # Limits the available syntax to the non-monkey patched syntax that is + # # recommended. For more details, see: + # # https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/ + # config.disable_monkey_patching! + # + # # This setting enables warnings. It's recommended, but in some cases may + # # be too noisy due to issues in dependencies. + # config.warnings = true + # + # # Many RSpec users commonly either run the entire suite or an individual + # # file, and it's useful to allow more verbose output when running an + # # individual spec file. + # if config.files_to_run.one? + # # Use the documentation formatter for detailed output, + # # unless a formatter has already been configured + # # (e.g. via a command-line flag). + # config.default_formatter = "doc" + # end + # + # # Print the 10 slowest examples and example groups at the + # # end of the spec run, to help surface which specs are running + # # particularly slow. + # config.profile_examples = 10 + # + # # Run specs in random order to surface order dependencies. If you find an + # # order dependency and want to debug it, you can fix the order by providing + # # the seed, which is printed after each run. + # # --seed 1234 + # config.order = :random + # + # # Seed global randomization in this process using the `--seed` CLI option. + # # Setting this allows you to use `--seed` to deterministically reproduce + # # test failures related to randomization by passing the same `--seed` value + # # as the one that triggered the failure. + # Kernel.srand config.seed +end diff --git a/test/e2e/spec/corner_cases_spec.rb b/test/e2e/spec/corner_cases_spec.rb new file mode 100644 index 0000000..f1111da --- /dev/null +++ b/test/e2e/spec/corner_cases_spec.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require_relative "../helper/command_helper" + +describe "Corner cases for push_swap" do + it "does not output operations for already sorted input" do + stdout, _stderr, status = execute_push_swap("0", "1", "2", "3") + expect(stdout).to(eq("")) + expect(status.exitstatus).to(eq(0)) + end +end diff --git a/test/e2e/spec/error_cases_spec.rb b/test/e2e/spec/error_cases_spec.rb new file mode 100644 index 0000000..e63d4bd --- /dev/null +++ b/test/e2e/spec/error_cases_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require_relative "../helper/command_helper" + +describe "Error cases for push_swap" do + it "returns error for non integer inputs" do + _stdout, stderr, status = execute_push_swap("a", "b", "c") + + expect(stderr).to(eq("Error\n")) + expect(status.exitstatus).to(eq(1)) + end + + it "returns error for duplicates" do + _stdout, stderr, status = execute_push_swap("3", "2", "5", "4", "1", "0", "2") + + expect(stderr).to(eq("Error\n")) + expect(status.exitstatus).to(eq(1)) + end + + it "returns error for overflow" do + _stdout, stderr, status = execute_push_swap("0", "3", "2", "34578347984", "1") + + expect(stderr).to(eq("Error\n")) + expect(status.exitstatus).to(eq(1)) + end +end diff --git a/test/e2e/spec/performance_spec.rb b/test/e2e/spec/performance_spec.rb new file mode 100644 index 0000000..aff88f2 --- /dev/null +++ b/test/e2e/spec/performance_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require_relative "../helper/command_helper" + +describe("Performance test for push_swap") do + size = 500 + it "measures operations for random input of size #{size}" do + results = [] + 20.times do + input = (0...500).to_a.shuffle.map(&:to_s) + stdout, _stderr, status = execute_push_swap(*input) + expect(status.exitstatus).to(eq(0)) + results << stdout.split("\n").count + + stdout, _stderr, status = execute_checker(*input) + expect(status.exitstatus).to(eq(0)) + expect(stdout).to(eq("OK\n")) + print "\e[32m.\e[0m" + end + max_operations = results.max + min_operations = results.min + average_operations = results.sum / results.size.to_f + print "\n" + print "For size #{size}: Max: #{max_operations}, Min: #{min_operations}, Average: #{average_operations}" + end +end diff --git a/test/pain.sh b/test/pain.sh new file mode 100755 index 0000000..5728bd9 --- /dev/null +++ b/test/pain.sh @@ -0,0 +1,16 @@ +#!/bin/bash +NBR_COUNT=500 +## +NBR=$(seq 1 $NBR_COUNT | sort -R | tr '\n' ' ' | rev | cut -c 2- | rev) +#NBR="0 1 2 3 4 5 6 7 8 9" +#NBR=$(seq 1 $NBR_COUNT | tr '\n' ' ' | rev | cut -c 2- | rev) +CMD=$( ./push_swap $NBR) +NBR_LINK=$( echo "$NBR" | tr ' ' ',' ) +CMD_LINK=$( echo "$CMD" | sed -e"s/rra/g/g" -e"s/rrb/h/g" \ +-e"s/rrr/i/g" -e"s/sa/a/g" -e"s/sb/b/g" -e"s/ss/c/g" \ +-e"s/ra/d/g" -e"s/rb/e/g" -e"s/rr/f/g" -e"s/pa/j/g" -e"s/pb/k/g" | tr -d '\n') +# CMD_COUNT=${#CMD_LINK} +RESULT=$( echo $CMD | ./checker $NBR ) +echo "Operations: $CMD_COUNT" +echo "Result $RESULT" +open "https://kaaaaakun.github.io/push-swap-pain/?&nbr=$NBR_LINK&cmd=$CMD_LINK" diff --git a/test/test_check_args.cpp b/test/unit/test_check_args.cpp similarity index 100% rename from test/test_check_args.cpp rename to test/unit/test_check_args.cpp diff --git a/test/test_greedy_operation.cpp b/test/unit/test_greedy_operation.cpp similarity index 100% rename from test/test_greedy_operation.cpp rename to test/unit/test_greedy_operation.cpp diff --git a/test/test_is_sorted_stack.cpp b/test/unit/test_is_sorted_stack.cpp similarity index 100% rename from test/test_is_sorted_stack.cpp rename to test/unit/test_is_sorted_stack.cpp diff --git a/test/test_push_b_segmented.cpp b/test/unit/test_push_b_segmented.cpp similarity index 100% rename from test/test_push_b_segmented.cpp rename to test/unit/test_push_b_segmented.cpp diff --git a/test/test_push_stack.cpp b/test/unit/test_push_stack.cpp similarity index 100% rename from test/test_push_stack.cpp rename to test/unit/test_push_stack.cpp diff --git a/test/test_reverse_rotate_stack.cpp b/test/unit/test_reverse_rotate_stack.cpp similarity index 100% rename from test/test_reverse_rotate_stack.cpp rename to test/unit/test_reverse_rotate_stack.cpp diff --git a/test/test_rotate_stack.cpp b/test/unit/test_rotate_stack.cpp similarity index 100% rename from test/test_rotate_stack.cpp rename to test/unit/test_rotate_stack.cpp diff --git a/test/test_set_cost.cpp b/test/unit/test_set_cost.cpp similarity index 100% rename from test/test_set_cost.cpp rename to test/unit/test_set_cost.cpp diff --git a/test/test_set_is_target.cpp b/test/unit/test_set_is_target.cpp similarity index 100% rename from test/test_set_is_target.cpp rename to test/unit/test_set_is_target.cpp diff --git a/test/test_set_min_cost_opt_method.cpp b/test/unit/test_set_min_cost_opt_method.cpp similarity index 100% rename from test/test_set_min_cost_opt_method.cpp rename to test/unit/test_set_min_cost_opt_method.cpp diff --git a/test/test_sort.cpp b/test/unit/test_sort.cpp similarity index 100% rename from test/test_sort.cpp rename to test/unit/test_sort.cpp diff --git a/test/test_swap_stack.cpp b/test/unit/test_swap_stack.cpp similarity index 100% rename from test/test_swap_stack.cpp rename to test/unit/test_swap_stack.cpp