fn_intersect.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2015 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2015 National ICT Australia (NICTA)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // ------------------------------------------------------------------------
  15. #include <armadillo>
  16. #include "catch.hpp"
  17. using namespace arma;
  18. TEST_CASE("fn_intersect_1")
  19. {
  20. ivec A = regspace<ivec>(5, 1); // 5, 4, 3, 2, 1
  21. ivec B = regspace<ivec>(3, 7); // 3, 4, 5, 6, 7
  22. ivec C = intersect(A,B); // 3, 4, 5
  23. REQUIRE( C(0) == 3 );
  24. REQUIRE( C(1) == 4 );
  25. REQUIRE( C(2) == 5 );
  26. REQUIRE( accu(C) == 12 );
  27. ivec CC;
  28. uvec iA;
  29. uvec iB;
  30. intersect(CC, iA, iB, A, B);
  31. REQUIRE( accu(abs(C-CC)) == 0 );
  32. REQUIRE( iA(0) == 2 );
  33. REQUIRE( iA(1) == 1 );
  34. REQUIRE( iA(2) == 0 );
  35. REQUIRE( accu(iA) == 3 );
  36. REQUIRE( iB(0) == 0 );
  37. REQUIRE( iB(1) == 1 );
  38. REQUIRE( iB(2) == 2 );
  39. REQUIRE( accu(iB) == 3 );
  40. }
  41. TEST_CASE("fn_intersect_2")
  42. {
  43. irowvec A = regspace<irowvec>(5, 1); // 5, 4, 3, 2, 1
  44. irowvec B = regspace<irowvec>(3, 7); // 3, 4, 5, 6, 7
  45. irowvec C = intersect(A,B); // 3, 4, 5
  46. REQUIRE( C(0) == 3 );
  47. REQUIRE( C(1) == 4 );
  48. REQUIRE( C(2) == 5 );
  49. REQUIRE( accu(C) == 12 );
  50. irowvec CC;
  51. uvec iA;
  52. uvec iB;
  53. intersect(CC, iA, iB, A, B);
  54. REQUIRE( accu(abs(C-CC)) == 0 );
  55. REQUIRE( iA(0) == 2 );
  56. REQUIRE( iA(1) == 1 );
  57. REQUIRE( iA(2) == 0 );
  58. REQUIRE( accu(iA) == 3 );
  59. REQUIRE( iB(0) == 0 );
  60. REQUIRE( iB(1) == 1 );
  61. REQUIRE( iB(2) == 2 );
  62. REQUIRE( accu(iB) == 3 );
  63. }
  64. TEST_CASE("fn_intersect_3")
  65. {
  66. irowvec A = regspace<irowvec>(5, 1);
  67. irowvec B = regspace<irowvec>(3, 7);
  68. ivec C;
  69. REQUIRE_THROWS( C = intersect(A,B) );
  70. }