#
# Copyright (c) 2009 D. Richard Hipp
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the Simplified BSD License (also
# known as the "2-Clause License" or "FreeBSD License".)
#
# This program is distributed in the hope that it will be useful,
# but without any warranty; without even the implied warranty of
# merchantability or fitness for a particular purpose.
#
# Author contact information:
#   drh@hwaci.com
#   http://www.hwaci.com/drh/
#
############################################################################
#
# Tests of the 3-way merge
#

test_setup ""

proc merge-test {testid basis v1 v2 result {fossil_args ""}} {
  write_file t1 [join [string trim $basis] \n]\n
  write_file t2 [join [string trim $v1] \n]\n
  write_file t3 [join [string trim $v2] \n]\n
  fossil 3-way-merge t1 t2 t3 t4 {*}$fossil_args
  set x [read_file t4]
  regsub -all \
    {<<<<<<< BEGIN MERGE CONFLICT: local copy shown first <+ \(line \d+\)} \
    $x {MINE:} x
  regsub -all \
    {####### SUGGESTED CONFLICT RESOLUTION follows #+} \
    $x {BOT:} x
  regsub -all \
    {\|\|\|\|\|\|\| COMMON ANCESTOR content follows \|+ \(line \d+\)} \
    $x {COM:} x
  regsub -all \
    {======= MERGED IN content follows =+ \(line \d+\)} \
    $x {YOURS:} x
  regsub -all \
    {>>>>>>> END MERGE CONFLICT >+} \
    $x {END} x
  set x [split [string trim $x] \n]
  set result [string trim $result]
  if {$x!=$result} {
    protOut "  Expected \[$result\]"
    protOut "       Got \[$x\]"
    test merge3-$testid 0
  } else {
    test merge3-$testid 1
  }
}

merge-test 1 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3b 4 5 6 7 8 9
} {
  1 2 3 4 5c 6 7 8 9
} {
  1 2 3b 4 5c 6 7 8 9
}

merge-test 2 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3b 4b 5 6 7 8 9
} {
  1 2 3 4 5c 6 7 8 9
} {
  1 2 3b 4b 5c 6 7 8 9
}
merge-test 3 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3b 4b 5b 6 7 8 9
} {
  1 2 3 4 5c 6 7 8 9
} {
  1 2 MINE: 3b 4b 5b BOT: 3b 4b 5c COM: 3 4 5 YOURS: 3 4 5c END 6 7 8 9
} -expectError
merge-test 4 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3b 4b 5b 6b 7 8 9
} {
  1 2 3 4 5c 6 7 8 9
} {
  1 2 MINE: 3b 4b 5b 6b BOT: 3b 4b 5b 5c 6 COM: 3 4 5 6 YOURS: 3 4 5c 6 END 7 8 9
} -expectError
merge-test 5 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3b 4b 5b 6b 7 8 9
} {
  1 2 3 4 5c 6c 7c 8 9
} {
  1 2 MINE: 3b 4b 5b 6b 7 BOT: 3b 4b 5b 5c 6c 7c COM: 3 4 5 6 7 YOURS: 3 4 5c 6c 7c END 8 9
} -expectError
merge-test 6 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3b 4b 5b 6b 7 8b 9
} {
  1 2 3 4 5c 6c 7c 8 9
} {
  1 2 MINE: 3b 4b 5b 6b 7 BOT: 3b 4b 5b 5c 6c 7c COM: 3 4 5 6 7 YOURS: 3 4 5c 6c 7c END 8b 9
} -expectError
merge-test 7 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3b 4b 5b 6b 7 8b 9
} {
  1 2 3 4 5c 6c 7c 8c 9
} {
  1 2 MINE: 3b 4b 5b 6b 7 8b BOT: 3b 4b 5b 5c 6c 7c 8c COM: 3 4 5 6 7 8 YOURS: 3 4 5c 6c 7c 8c END 9
} -expectError
merge-test 8 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3b 4b 5b 6b 7 8b 9b
} {
  1 2 3 4 5c 6c 7c 8c 9
} {
  1 2 MINE: 3b 4b 5b 6b 7 8b 9b BOT: 3b 4b 5b 5c 6c 7c 8c 9b COM: 3 4 5 6 7 8 9 YOURS: 3 4 5c 6c 7c 8c 9 END
} -expectError
merge-test 9 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3b 4b 5 6 7 8b 9b
} {
  1 2 3 4 5c 6c 7c 8 9
} {
  1 2 3b 4b 5c 6c 7c 8b 9b
}

merge-test 10 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3b 4b 5 6 7 8b 9b
} {
  1 2 3b 4b 5 6c 7c 8 9
} {
  1 2 3b 4b 5 6c 7c 8b 9b
}
merge-test 11 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3b 4b 5 6 7 8b 9b
} {
  1 2 3b 4c 5 6c 7c 8 9
} {
  1 2 MINE: 3b 4b BOT: 3b 4c COM: 3 4 YOURS: 3b 4c END 5 6c 7c 8b 9b
} -expectError
merge-test 12 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3b4b 5 6 7 8b 9b
} {
  1 2 3b4b 5 6c 7c 8 9
} {
  1 2 3b4b 5 6c 7c 8b 9b
}

merge-test 20 {
  1 2 3 4 5 6 7 8 9
} {
  1 3 4 5 6 7 8 9
} {
  1 2 3 4 5 6 7 9
} {
  1 3 4 5 6 7 9
}
merge-test 21 {
  1 2 3 4 5 6 7 8 9
} {
  1 4 5 6 7 8 9
} {
  1 2 3 4 5 6 9
} {
  1 4 5 6 9
}
merge-test 22 {
  1 2 3 4 5 6 7 8 9
} {
  1 5 6 7 8 9
} {
  1 2 3 4 5 9
} {
  1 5 9
}
merge-test 23 {
  1 2 3 4 5 6 7 8 9
} {
  1 6 7 8 9
} {
  1 2 3 4 5 9
} {
  1 9
}
merge-test 24 {
  1 2 3 4 5 6 7 8 9
} {
  1 6 7 8 9
} {
  1 2 3 4 9
} {
  1 MINE: 6 7 8 BOT: 2 3 4 COM: 2 3 4 5 6 7 8 YOURS: 2 3 4 END 9
} -expectError
merge-test 25 {
  1 2 3 4 5 6 7 8 9
} {
  1 7 8 9
} {
  1 2 3 9
} {
  1 MINE: 7 8 BOT: 2 3 COM: 2 3 4 5 6 7 8 YOURS: 2 3 END 9
} -expectError

merge-test 30 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3 4 5 6 7 9
} {
  1 3 4 5 6 7 8 9
} {
  1 3 4 5 6 7 9
}
merge-test 31 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3 4 5 6 9
} {
  1 4 5 6 7 8 9
} {
  1 4 5 6 9
}
merge-test 32 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3 4 5 9
} {
  1 5 6 7 8 9
} {
  1 5 9
}
merge-test 33 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3 4 5 9
} {
  1 6 7 8 9
} {
  1 9
}
merge-test 34 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3 4 9
} {
  1 6 7 8 9
} {
  1 MINE: 2 3 4 BOT: 6 7 8 COM: 2 3 4 5 6 7 8 YOURS: 6 7 8 END 9
} -expectError
merge-test 35 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3 9
} {
  1 7 8 9
} {
  1 MINE: 2 3 BOT: 7 8 COM: 2 3 4 5 6 7 8 YOURS: 7 8 END 9
} -expectError

merge-test 40 {
  2 3 4 5 6 7 8
} {
  3 4 5 6 7 8
} {
  2 3 4 5 6 7
} {
  3 4 5 6 7
}
merge-test 41 {
  2 3 4 5 6 7 8
} {
  4 5 6 7 8
} {
  2 3 4 5 6
} {
  4 5 6
}
merge-test 42 {
  2 3 4 5 6 7 8
} {
  5 6 7 8
} {
  2 3 4 5
} {
  5
}
merge-test 43 {
  2 3 4 5 6 7 8
} {
  6 7 8
} {
  2 3 4 5
} {
  
}
merge-test 44 {
  2 3 4 5 6 7 8
} {
  6 7 8
} {
  2 3 4
} {
  MINE: 6 7 8 BOT: 2 3 4 COM: 2 3 4 5 6 7 8 YOURS: 2 3 4 END
} -expectError
merge-test 45 {
  2 3 4 5 6 7 8
} {
  7 8
} {
  2 3
} {
  MINE: 7 8 BOT: 2 3 COM: 2 3 4 5 6 7 8 YOURS: 2 3 END
} -expectError

merge-test 50 {
  2 3 4 5 6 7 8
} {
  2 3 4 5 6 7
} {
  3 4 5 6 7 8
} {
  3 4 5 6 7
}
merge-test 51 {
  2 3 4 5 6 7 8
} {
  2 3 4 5 6
} {
  4 5 6 7 8
} {
  4 5 6
}
merge-test 52 {
  2 3 4 5 6 7 8
} {
  2 3 4 5
} {
  5 6 7 8
} {
  5
}
merge-test 53 {
  2 3 4 5 6 7 8
} {
  2 3 4 5
} {
  6 7 8
} {
}
merge-test 54 {
  2 3 4 5 6 7 8
} {
  2 3 4
} {
  6 7 8
} {
  MINE: 2 3 4 BOT: 6 7 8 COM: 2 3 4 5 6 7 8 YOURS: 6 7 8 END
} -expectError
merge-test 55 {
  2 3 4 5 6 7 8
} {
  2 3
} {
  7 8
} {
  MINE: 2 3 BOT: 7 8 COM: 2 3 4 5 6 7 8 YOURS: 7 8 END
} -expectError

merge-test 60 {
  1 2 3 4 5 6 7 8 9
} {
  1 2b 3 4 5 6 7 8 9
} {
  1 2 3 4 5 6 7 9
} {
  1 2b 3 4 5 6 7 9
}
merge-test 61 {
  1 2 3 4 5 6 7 8 9
} {
  1 2b 3b 4 5 6 7 8 9
} {
  1 2 3 4 5 6 9
} {
  1 2b 3b 4 5 6 9
}
merge-test 62 {
  1 2 3 4 5 6 7 8 9
} {
  1 2b 3b 4b 5 6 7 8 9
} {
  1 2 3 4 5 9
} {
  1 2b 3b 4b 5 9
}
merge-test 63 {
  1 2 3 4 5 6 7 8 9
} {
  1 2b 3b 4b 5b 6 7 8 9
} {
  1 2 3 4 5 9
} {
  1 2b 3b 4b 5b 9
}
merge-test 64 {
  1 2 3 4 5 6 7 8 9
} {
  1 2b 3b 4b 5b 6 7 8 9
} {
  1 2 3 4 9
} {
  1 MINE: 2b 3b 4b 5b 6 7 8 BOT: 2b 3b 4b 4 COM: 2 3 4 5 6 7 8 YOURS: 2 3 4 END 9
} -expectError
merge-test 65 {
  1 2 3 4 5 6 7 8 9
} {
  1 2b 3b 4b 5b 6b 7 8 9
} {
  1 2 3 9
} {
  1 MINE: 2b 3b 4b 5b 6b 7 8 BOT: 2 3 COM: 2 3 4 5 6 7 8 YOURS: 2 3 END 9
} -expectError

merge-test 70 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3 4 5 6 7 9
} {
  1 2b 3 4 5 6 7 8 9
} {
  1 2b 3 4 5 6 7 9
}
merge-test 71 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3 4 5 6 9
} {
  1 2b 3b 4 5 6 7 8 9
} {
  1 2b 3b 4 5 6 9
}
merge-test 72 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3 4 5 9
} {
  1 2b 3b 4b 5 6 7 8 9
} {
  1 2b 3b 4b 5 9
}
merge-test 73 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3 4 5 9
} {
  1 2b 3b 4b 5b 6 7 8 9
} {
  1 2b 3b 4b 5b 9
}
merge-test 74 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3 4 9
} {
  1 2b 3b 4b 5b 6 7 8 9
} {
  1 MINE: 2 3 4 BOT: 2b 3b 4b 5b 6 7 8 COM: 2 3 4 5 6 7 8 YOURS: 2b 3b 4b 5b 6 7 8 END 9
} -expectError
merge-test 75 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3 9
} {
  1 2b 3b 4b 5b 6b 7 8 9
} {
  1 MINE: 2 3 BOT: 2b 3b 4b 5b 6b 7 8 COM: 2 3 4 5 6 7 8 YOURS: 2b 3b 4b 5b 6b 7 8 END 9
} -expectError

merge-test 80 {
  2 3 4 5 6 7 8
} {
  2b 3 4 5 6 7 8
} {
  2 3 4 5 6 7
} {
  2b 3 4 5 6 7
}
merge-test 81 {
  2 3 4 5 6 7 8
} {
  2b 3b 4 5 6 7 8
} {
  2 3 4 5 6
} {
  2b 3b 4 5 6
}
merge-test 82 {
  2 3 4 5 6 7 8
} {
  2b 3b 4b 5 6 7 8
} {
  2 3 4 5
} {
  2b 3b 4b 5
}
merge-test 83 {
  2 3 4 5 6 7 8
} {
  2b 3b 4b 5b 6 7 8
} {
  2 3 4 5
} {
  2b 3b 4b 5b
}
merge-test 84 {
  2 3 4 5 6 7 8
} {
  2b 3b 4b 5b 6 7 8
} {
  2 3 4
} {
  MINE: 2b 3b 4b 5b 6 7 8 BOT: 2b 3b 4b 4 COM: 2 3 4 5 6 7 8 YOURS: 2 3 4 END
} -expectError
merge-test 85 {
  2 3 4 5 6 7 8
} {
  2b 3b 4b 5b 6b 7 8
} {
  2 3
} {
  MINE: 2b 3b 4b 5b 6b 7 8 BOT: 2 3 COM: 2 3 4 5 6 7 8 YOURS: 2 3 END
} -expectError

merge-test 90 {
  2 3 4 5 6 7 8
} {
  2 3 4 5 6 7
} {
  2b 3 4 5 6 7 8
} {
  2b 3 4 5 6 7
}
merge-test 91 {
  2 3 4 5 6 7 8
} {
  2 3 4 5 6
} {
  2b 3b 4 5 6 7 8
} {
  2b 3b 4 5 6
}
merge-test 92 {
  2 3 4 5 6 7 8
} {
  2 3 4 5
} {
  2b 3b 4b 5 6 7 8
} {
  2b 3b 4b 5
}
merge-test 93 {
  2 3 4 5 6 7 8
} {
  2 3 4 5
} {
  2b 3b 4b 5b 6 7 8
} {
  2b 3b 4b 5b
}
merge-test 94 {
  2 3 4 5 6 7 8
} {
  2 3 4
} {
  2b 3b 4b 5b 6 7 8
} {
  MINE: 2 3 4 BOT: 2b 3b 4b 5b 6 7 8 COM: 2 3 4 5 6 7 8 YOURS: 2b 3b 4b 5b 6 7 8 END
} -expectError
merge-test 95 {
  2 3 4 5 6 7 8
} {
  2 3
} {
  2b 3b 4b 5b 6b 7 8
} {
  MINE: 2 3 BOT: 2b 3b 4b 5b 6b 7 8 COM: 2 3 4 5 6 7 8 YOURS: 2b 3b 4b 5b 6b 7 8 END
} -expectError

merge-test 100 {
  1 2 3 4 5 6 7 8 9
} {
  1 2b 3 4 5 7 8 9 a b c d e
} {
  1 2b 3 4 5 7 8 9 a b c d e
} {
  1 2b 3 4 5 7 8 9 a b c d e
}
merge-test 101 {
  1 2 3 4 5 6 7 8 9
} {
  1 2b 3 4 5 7 8 9 a b c d e
} {
  1 2b 3 4 5 7 8 9
} {
  1 2b 3 4 5 7 8 9 a b c d e
}
merge-test 102 {
  1 2 3 4 5 6 7 8 9
} {
  1 2b 3 4 5 7 8 9
} {
  1 2b 3 4 5 7 8 9 a b c d e
} {
  1 2b 3 4 5 7 8 9 a b c d e
}
merge-test 103 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3 4 5 7 8 9b
} {
  1 2 3 4 5 7 8 9b a b c d e
} {
  1 2 3 4 5 7 8 MINE: 9b BOT: 9b a b c d e COM: 9 YOURS: 9b a b c d e END
} -expectError
merge-test 104 {
  1 2 3 4 5 6 7 8 9
} {
  1 2 3 4 5 7 8 9b a b c d e
} {
  1 2 3 4 5 7 8 9b
} {
  1 2 3 4 5 7 8 MINE: 9b a b c d e BOT: 9b COM: 9 YOURS: 9b END
} -expectError

###############################################################################

test_cleanup
